fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long int
  4. #define double long double
  5. inline int power(int a, int b) {
  6. int x = 1;
  7. while (b) {
  8. if (b & 1) x *= a;
  9. a *= a;
  10. b >>= 1;
  11. }
  12. return x;
  13. }
  14.  
  15.  
  16. const int M = 1000000007;
  17. const int N = 3e5+9;
  18. const int INF = 2e9+1;
  19. const int LINF = 2000000000000000001;
  20.  
  21. //_ ***************************** START Below *******************************
  22.  
  23.  
  24.  
  25. vector<int> a;
  26.  
  27.  
  28. int consistency(int n) {
  29.  
  30. int s = 0, e = n-1;
  31. int p1 = 0, p2 = 0;
  32.  
  33. bool isReversed = false;
  34. int turn = 1; //* a's turn
  35.  
  36. while(s<=e){
  37. int ele = -1;
  38. if(isReversed){
  39. //* Consume from end
  40. ele = a[e];
  41. e--;
  42. }
  43. else{
  44. //* Consume normal i.e. from s
  45. ele = a[s];
  46. s++;
  47. }
  48.  
  49. if(!(ele & 1)){
  50. isReversed = !isReversed;
  51. }
  52. if(turn) p1 += ele;
  53. else p2 += ele;
  54. turn ^= 1;
  55. }
  56.  
  57. return p1 - p2;
  58.  
  59. }
  60.  
  61.  
  62.  
  63.  
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72. int practice(int n) {
  73.  
  74. return 0;
  75.  
  76. }
  77.  
  78.  
  79.  
  80. void solve() {
  81.  
  82. int n;
  83. cin >> n;
  84.  
  85. a.resize(n);
  86. for(int i=0; i<n; i++) cin >> a[i];
  87.  
  88. // cout << consistency(n) << endl;
  89.  
  90.  
  91. cout << consistency(n) << " -> " << practice(n) << endl;
  92.  
  93. }
  94.  
  95.  
  96.  
  97.  
  98.  
  99. int32_t main() {
  100. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  101.  
  102. int t = 1;
  103. while (t--) {
  104. solve();
  105. }
  106.  
  107. return 0;
  108. }
Success #stdin #stdout 0.01s 5316KB
stdin
4
2 1 4 3
stdout
2 -> 2