This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
long long int n,a[200005],b[200005],bra,brb,cycle;
void rec(int num,long long int mah,int br,int cycle)
{
if(cycle==100000000)
{
cout<<"No"<<endl;
return;
}
if(num==0)num=n;
if(num>n)num=1;
if(br==n)
{
cout<<"Yes"<<endl;
return;
}
a[num]-=mah;
bra-=mah;
if(bra<brb)
{
cout<<"No"<<endl;
return;
}
/*cout<<num<<" "<<mah<<" "<<br<<endl;
for(long long int i=1;i<=n;i++)
{
cout<<a[i]<<" ";
}
cout<<endl;
*/
if(a[num]==b[num])
{
rec(num+1,0,br+1,cycle+1);
}
else if(a[num]-b[num]>0)
{
if((a[num]-b[num])%2==0)
{
long long int h=(a[num]-b[num]);
a[num]=b[num];
rec(num+1,-h/2,0,cycle+1);
}
else
{
if(a[num]==1)
{
rec(num-1,2,0,cycle+1);
return;
}
long long int h=(a[num]-b[num]);
a[num]=b[num]-1;
rec(num+1,-h/2-1,0,cycle+1);
}
}
else
{
rec(num-1,(b[num]-a[num])*2,0,cycle+1);
}
}
void read()
{
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>a[i]>>b[i];
bra+=a[i];
brb+=b[i];
}
rec(1,0,0,0);
}
int main()
{
speed();
read();
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |