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;
  31. int e = n-1;
  32. int turn = 0;
  33. bool rev = false;
  34.  
  35. int p1 = 0;
  36. int p2 = 0;
  37.  
  38. while(s<=e){
  39.  
  40. int val;
  41. if(rev){
  42. val = a[e];
  43. e--;
  44. }
  45. else{
  46. val = a[s];
  47. s++;
  48. }
  49.  
  50. if( !(val & 1)){
  51. rev = !rev;
  52. }
  53.  
  54. if(turn == 0){
  55. p1 += val;
  56. }
  57. else p2 += val;
  58.  
  59. turn ^= 1;
  60.  
  61. }
  62.  
  63. return p1-p2;
  64.  
  65. }
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73.  
  74.  
  75.  
  76.  
  77.  
  78. int practice(int n) {
  79.  
  80.  
  81. }
  82.  
  83.  
  84.  
  85. void solve() {
  86.  
  87. int n;
  88. cin >> n;
  89.  
  90. a.resize(n);
  91. for(int i=0; i<n; i++) cin >> a[i];
  92.  
  93. cout << consistency(n) << endl;
  94.  
  95.  
  96. // cout << consistency(n) << " -> " << practice(n) << endl;
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. int32_t main() {
  105. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  106.  
  107. int t = 1;
  108. while (t--) {
  109. solve();
  110. }
  111.  
  112. return 0;
  113. }
Success #stdin #stdout 0s 5324KB
stdin
4
2 1 4 3
stdout
2