This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// Be name khoda //
#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
#define pb push_back
#define fi first
#define se second
#define mp make_pair
typedef long long ll;
using namespace std;
const int maxn5 = 1e6 + 10;
const ll inf = 1e18;
bool mark[maxn5];
ll a[maxn5], b[maxn5];
queue <int> q;
int n;
bool found = false;
void solve(){
bool re = true;
for(int i = 0; i < n; i++)
re &= (a[i] == b[i]);
if(re){
found = true;
return;
}
for(int i = 0; i < n; i++) for(int x = 2; x <= a[i]; x+= 2){
a[i] -= x;
a[(i + 1)%n] += x / 2;
solve();
if(found)
return;
a[i] += x;
a[(i + 1)%n] -= x / 2;
}
}
int main(){
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
ll sum = 0;
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
sum += a[i];
}
solve();
cout << (found ? "Yes" : "No") << endl;
return 0;
while(sum > 0){
bool re = true;
for(int i = 0; i < n; i++){
if(a[i] < b[i]){
int j = (i - 1 + n) % n;
sum -= b[i] - a[i];
a[j] -= 2 * (b[i] - a[i]);
a[i] = b[i];
re = false;
}
else if(a[i] > b[i]){
int j = (i + 1) % n;
ll w = (a[i] - b[i]) / 2 + ((a[i] - b[i]) % 2);
a[j] += w;
a[i] -= 2 * w;
sum -= w;
re = false;
}
}
if(re)
return cout << "Yes" << endl, 0;
}
cout << "No" << endl;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |