#include<bits/stdc++.h>
#define mp make_pair
#define F first
#define S second
#define printv(a, b) { \
for(auto pv : a) b << pv << " "; \
b << "\n";\
}
using namespace std;
typedef long long ll;
using pll = pair<ll, ll>;
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
int n;
cin >> n;
vector<ll> a(n), b(n);
ll diff = 0;
set<pll, greater<>> s;
auto rmv = [&](int i){
if(a[i] >= 2 && a[i] - b[i] != 0) s.erase(mp(a[i] - b[i], i));
};
auto add = [&](int i){
if(a[i] >= 2 && a[i] - b[i] != 0) s.insert(mp(a[i] - b[i], i));
};
for(int i = 0; i < n; i++){
cin >> a[i] >> b[i];
add(i);
diff += b[i] - a[i];
}
//cerr << "diff " << diff << "\n";
if(diff % 2 || diff > 0){
cout << "No\n";
return 0;
}
while(!s.empty()){
pll now = *s.begin();
int id = now.S;
s.erase(s.begin());
//cerr << "test " << now.F << " " << now.S << "\n";
ll cnt = now.F;
if(cnt < 0) break;
cnt = max(cnt / 2, 1LL);
int nxt = (id + 1) % n;
//cerr << cnt << " " << nxt << "\n";
rmv(nxt);
a[id] -= cnt * 2;
a[nxt] += cnt;
add(nxt);
add(id);
//printv(a, cerr);
//printv(b, cerr);
}
for(int i = 0; i < n; i++){
if(a[i] != b[i]){
cout << "No\n";
return 0;
}
}
cout << "Yes\n";
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
208 KB |
Output is correct |
2 |
Correct |
1 ms |
208 KB |
Output is correct |
3 |
Correct |
0 ms |
208 KB |
Output is correct |
4 |
Incorrect |
1 ms |
208 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |