fork download
  1. #include<iostream>
  2. #include<vector>
  3. #include<algorithm>
  4. using namespace std;
  5. int main() {
  6. int i,j,k,c=0,n,m,d=0,e=0,f,g=0;
  7. cin>>n;
  8. f=n-1;
  9. vector<int> a(n);
  10. for(i=0;i<n;i++)
  11. {
  12. cin>>a[i];
  13. }
  14. for(i=0;i<n;)
  15. {
  16. c++;
  17. if(c==n)
  18. break;
  19. if(a[e]>a[f])
  20. {
  21. if(c%2!=0)
  22. d+=a[e];
  23. else
  24. g+=a[e];
  25. e++;
  26. i++;
  27. }
  28. else if(a[e]<a[f])
  29. {
  30. if(c%2!=0)
  31. d+=a[f];
  32. else
  33. g+=a[f];
  34. f--;
  35. i++;
  36. }
  37. }
  38. if(c%2==0)
  39. cout<<d<<" "<<g+a[f];
  40. else
  41. cout<<d+a[e]<<" "<<g;
  42. return 0;
  43. }
Success #stdin #stdout 0s 15224KB
stdin
42
15 29 37 22 16 5 26 31 6 32 19 3 45 36 33 14 25 20 48 7 42 11 24 28 9 18 8 21 47 17 38 40 44 4 35 1 43 39 41 27 12 13
stdout
613 418