fork download
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. #define int long long
  5. #define yes cout << "YES\n";
  6. #define no cout << "NO\n";
  7.  
  8.  
  9. void FastIO(){
  10. ios_base::sync_with_stdio(false);
  11. cin.tie(nullptr);
  12. cout.tie(nullptr);
  13. }
  14. /// Sereja and Dima
  15. void solve(){
  16. int n; cin >> n;
  17. vector<int>vec(n);
  18.  
  19. for(int i = 0; i < n; i++){
  20. cin >> vec[i];
  21. }
  22.  
  23. int sereja = 0, dima = 0, turn = 1, i = 0, j = n-1;
  24.  
  25. while(i<=j){
  26. if(turn%2 == 1){
  27. if(vec[i] > vec[j]){
  28. sereja += vec[i];
  29. i++;
  30. }
  31. else{
  32. sereja += vec[j];
  33. j--;
  34. }
  35. }
  36. else{
  37. if(vec[i] > vec[j]){
  38. dima += vec[i];
  39. i++;
  40. }
  41. else{
  42. dima += vec[j];
  43. j--;
  44. }
  45. }
  46. turn++;
  47. }
  48. cout << sereja << " " << dima;
  49. }
  50.  
  51. signed main(){
  52. FastIO();
  53.  
  54. int t = 1;
  55. //cin >> t;
  56.  
  57. while(t--){
  58. solve();
  59. }
  60. return 0;
  61. }
Success #stdin #stdout 0.01s 5284KB
stdin
7
1 2 3 4 5 6 7

stdout
16 12