Advertisement
jakaria_hossain

codeforces - alice, bob and candies

May 10th, 2020
1,246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define fast()(ios_base::sync_with_stdio(0),cin.tie(NULL));
  5. int main()
  6. {
  7.     ll t;
  8.     cin>>t;
  9.     while(t--)
  10.     {
  11.         ll n;
  12.         cin>>n;
  13.         ll ara[n+1];
  14.         for(ll i=0; i<n;i++)cin>> ara[i];
  15.         ll mov=1,alice=ara[0],bob=0,suma=ara[0],sumb=0;
  16.         ll i=1,j=n-1;
  17.         bool tog=true;
  18.         while(i<=j)
  19.         {
  20.             if(alice<bob)
  21.             {
  22.                 suma+=ara[i];
  23.                 alice+=ara[i];
  24.                 i++;
  25.                 if(!tog)mov++;
  26.                 tog=true;
  27.                 if(alice>bob)bob=0;
  28.             }
  29.             else if(bob<alice)
  30.             {
  31.                 sumb+=ara[j];
  32.                 bob+=ara[j];
  33.                 j--;
  34.                 if(tog)mov++;
  35.                 tog=false;
  36.                 if(bob>alice)alice=0;
  37.             }
  38.             else
  39.             {
  40.                 if(tog)
  41.                 {
  42.                     suma+=ara[i];
  43.                     alice+=ara[i];
  44.                     i++;
  45.                     bob=0;
  46.                 }
  47.                 else
  48.                 {
  49.                     sumb+=ara[j];
  50.                     bob+=ara[j];
  51.                     j--;
  52.                     alice=0;
  53.                 }
  54.  
  55.             }
  56.  
  57.         }
  58.         cout<<mov<<" "<<suma<<" "<<sumb<<endl;
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement