fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. void solve(int a[], int n){
  5. int scoreA = 0, scoreB = 0;
  6. int turn = 0;
  7.  
  8. for(int i = 0; i < n; i++){
  9. int max_val = -1;
  10. int max_idx = -1;
  11.  
  12. // 最大値とそのインデックスを探す
  13. for(int j = 0; j < n; j++){
  14. if(a[j] > max_val){
  15. max_val = a[j];
  16. max_idx = j;
  17. }
  18. }
  19.  
  20. if(max_idx == -1) break; // すべて0なら終了
  21.  
  22. // スコア加算
  23. if(turn % 2 == 0){
  24. scoreA += max_val;
  25. } else {
  26. scoreB += max_val;
  27. }
  28.  
  29. a[max_idx] = 0; // 使ったカードを0にする
  30. turn++;
  31. }
  32.  
  33. printf("A:%d\n", scoreA);
  34. printf("B:%d\n", scoreB);
  35. }
  36.  
  37. int main(void){
  38. int n, i;
  39. int *v;
  40.  
  41. scanf("%d", &n);
  42. v = (int*)malloc(sizeof(int) * n);
  43. if(v == NULL){
  44. printf("ERROR\n");
  45. return -1;
  46. }
  47.  
  48. for(i = 0; i < n; i++){
  49. scanf("%d", &v[i]);
  50. }
  51.  
  52. solve(v, n);
  53. free(v);
  54. return 0;
  55. }
  56.  
Success #stdin #stdout 0.01s 5304KB
stdin
8
21 55 5 13 8 2 34 3
stdout
A:87
B:54