fork download
  1. /*
  2. ==> Don't stop when you're tired, stop when you're done.
  3. --> @author: MIDORIYA_
  4. */
  5. //*==============================================================
  6. #include <bits/stdc++.h>
  7. using namespace std;
  8. typedef long long ll;
  9. typedef double db;
  10. typedef long double ld;
  11. typedef pair<int, int> pii;
  12. typedef vector<int> vi;
  13. typedef vector<ll> vll;
  14. typedef vector<db> vd;
  15. typedef vector<ld> vld;
  16. typedef vector<bool> vb;
  17. typedef vector<vector<ll>> vvl;
  18. typedef vector<pii> vii;
  19. typedef set<int> si;
  20. typedef set<ll> sl;
  21. #define pb push_back
  22. #define all(x) x.begin(), x.end()
  23. #define rall(x) x.rbegin(), x.rend()
  24. #define Green true
  25. #define Red false
  26. #define endl "\n"
  27. const ll mod = 1'000'000'007;
  28. #define INF 2'000'000'000
  29. #define time cerr << "Time Taken: " << (float)clock() / CLOCKS_PER_SEC << " Secs" << endl;
  30. //*===================>>>Fast-IO-Functions<<<=================
  31. void fastIO()
  32. {
  33. ios_base::sync_with_stdio(false);
  34. cin.tie(nullptr);
  35. cout.tie(nullptr);
  36. }
  37. //*===================>>>File-IO-Functions<<<=================
  38. void fileIO()
  39. {
  40. #ifndef ONLINE_JUDGE
  41. freopen("in.txt", "r", stdin);
  42. freopen("out.txt", "w", stdout);
  43. #endif
  44. }
  45. //*===================>>>ONE-FOR-ALL-Function<<<==============
  46. void OneForAll()
  47. {
  48. int n;
  49. cin >> n;
  50. vi arr(n);
  51. for (int i = 0; i < n; i++)
  52. {
  53. cin >> arr[i];
  54. }
  55.  
  56. int sereja = 0, Dima = 0;
  57.  
  58. int l = 0, r = n - 1; // left and right pointers
  59. while (l <= r)
  60. {
  61. // Sereja's turn
  62. if (arr[l] > arr[r])
  63. {
  64. // Sereja picks the leftmost element
  65. sereja += arr[l];
  66. l++;
  67. }
  68. else
  69. {
  70. // Sereja picks the rightmost element
  71. sereja += arr[r];
  72. r--;
  73. }
  74.  
  75. // Dima's turn
  76. if (l <= r)
  77. {
  78. // Dima picks the larger of the two remaining elements
  79. if (arr[l] > arr[r])
  80. {
  81. // Dima picks the leftmost element
  82. Dima += arr[l];
  83. l++;
  84. }
  85. else
  86. {
  87. // Dima picks the rightmost element
  88. Dima += arr[r];
  89. r--;
  90. }
  91. }
  92. }
  93.  
  94. cout << sereja << " " << Dima;
  95. }
  96.  
  97. int main()
  98. {
  99. fastIO();
  100. fileIO();
  101.  
  102. ll tc = 1;
  103. // cin >> tc;
  104. while (tc--)
  105. {
  106. OneForAll();
  107. }
  108. time;
  109. return 0;
  110. }
  111.  
Success #stdin #stdout #stderr 0.01s 5320KB
stdin
Standard input is empty
stdout
0 0
stderr
Time Taken: 0.006895 Secs