Advertisement
sajid161

2: 1 problem

Nov 29th, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.88 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define optimize() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  3. using namespace std;
  4. const int mx=1e3+123;
  5. int v[mx];
  6. int main()
  7. {
  8.     optimize();
  9.     int n;
  10.     cin>>n;
  11.     for(int i=0;i<n;i++)
  12.     {
  13.         cin>>v[i];
  14.     }
  15.  
  16.    int j=1,s=0,d=0,l=0,r=n-1;
  17.     while(l<=r)
  18.     {
  19.  
  20.         if(j==1)
  21.         {
  22.             if(v[l]>v[r])
  23.             {
  24.                 s+=v[l];
  25.                 l++;
  26.             }
  27.             else
  28.             {
  29.                 s+=v[r];
  30.                 r--;
  31.             }
  32.             j=2;
  33.           }
  34.             else if(j==2)
  35.            {
  36.             if(v[l]>v[r])
  37.             {
  38.                 d+=v[l];
  39.                 l++;
  40.             }
  41.             else
  42.             {
  43.                 d+=v[r];
  44.                 r--;
  45.             }
  46.             j=1;
  47.         }
  48.     }
  49.  
  50.     cout<<s<<" "<<d;
  51. }
  52.  
  53.  
  54.  
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement