fork download
  1. ///nhatnama4k68nq
  2.  
  3. ///---------------------------------------------------------------------
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. #define endl "\n"
  8. #define N 1000005
  9. #define NN 100005
  10. #define MOD 1000000007
  11. #define pb push_back
  12. #define pf push_front
  13. #define fi first
  14. #define se second
  15. #define FOR(i,a,b) for (int i = (a); i < (b); i++)
  16. #define FORd(i,a,b) for (int i = (a) - 1; i >= (b); i--)
  17.  
  18. ///---------------------------------------------------------------------
  19.  
  20.  
  21. typedef long long ll;
  22. typedef unsigned long long ull;
  23. typedef pair<int,int> pii;
  24. typedef vector<int> vi;
  25. typedef vector<pair<int,int>> vii;
  26.  
  27. ///---------------------------------------------------------------------
  28.  
  29. const int maxint = 1e9+ 1608;
  30. const long long maxll = 1e18+ 16082009;
  31.  
  32. ///---------------------------------------------------------------------
  33.  
  34. /**
  35. ------------------------------------------------------------------------
  36.  
  37.  
  38.   NHAT NAM A4K68NQ
  39.  
  40.  
  41. ------------------------------------------------------------------------
  42. */
  43.  
  44.  
  45. int n;
  46. bool an = true;
  47.  
  48. ll res = 0;
  49. deque<int> w;
  50. void Read(){
  51. cin >> n;
  52. FOR (i,0,n){
  53. int u; cin >> u;
  54. w.pb(u);
  55. }
  56. while (!w.empty()){
  57. if (an){
  58. if (w.front() > w.back()){
  59. int nho = w.back();
  60. res += nho;
  61. w.pop_back();
  62. }
  63. else{
  64. int nho = w.front();
  65. res += nho;
  66. w.pop_front();
  67. }
  68.  
  69. an = false;
  70. }
  71. else{
  72. an = true;
  73. if (w.front() > w.back()) w.pop_back();
  74. else w.pop_front();
  75. }
  76. }
  77. cout << res;
  78. }
  79.  
  80.  
  81. void Solve(){
  82.  
  83. }
  84.  
  85. int main(){
  86. // #define NAME "main"
  87. // freopen(NAME".INP", "r", stdin);
  88. // freopen(NAME".OUT", "w",stdout);
  89. int t = 1;
  90. while (t--){
  91. Read();
  92. Solve();
  93. }
  94. return 0;
  95. }
  96.  
Success #stdin #stdout 0.01s 5272KB
stdin
Standard input is empty
stdout
Standard output is empty