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. void consistency(int n) {
  27.  
  28. int s = 0, e = n-1;
  29. int curr = s;
  30. int p1 = 0, p2 = 0;
  31. int toggle = 1;
  32.  
  33. //* keep in mind => s < e (not s<=e)
  34. while(s<e){
  35. if(toggle){
  36. p1 += a[curr];
  37. }
  38. else{
  39. p2 += a[curr];
  40. }
  41. bool isEven = (a[curr] & 1) ? false : true;
  42. if(curr == s){
  43. s++;
  44. curr = s;
  45. }
  46. else{
  47. e--;
  48. curr = e;
  49. }
  50. if(isEven){
  51. if(curr == s) curr = e;
  52. else curr = s;
  53. }
  54. toggle ^= 1;
  55. }
  56.  
  57. cout << p1-p2 << endl;
  58.  
  59. }
  60.  
  61. void solve() {
  62.  
  63. int n;
  64. cin >> n;
  65. a.resize(n);
  66.  
  67. for(int i=0; i<n; i++) cin >> a[i];
  68.  
  69. consistency(n) ;
  70.  
  71. }
  72.  
  73.  
  74.  
  75.  
  76.  
  77. int32_t main() {
  78. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  79.  
  80. int t = 1;
  81. while (t--) {
  82. solve();
  83. }
  84.  
  85. return 0;
  86. }
Success #stdin #stdout 0.01s 5320KB
stdin
2
2 2
stdout
2