#include <bits/stdc++.h>
// solved by bekagg
#define int long long
#define ent '\n'
#define pb push_back
#define all(x) x.begin(),x.end()
#define PRaim_bek_abi ios_base::sync_with_stdio(0);cin.tie(0);
using namespace std;
const int N = 1e5+5;
const int MOD = 1e9+7;
int n , m;
void arkanefury228(){
cin >> n;
multiset<pair<int , int> , greater< pair<int ,int> > >a;
for (int i = 1; i <= n; i++){
int x , y;
cin >> x >> y;
if (x == 0) continue;
a.insert({x , y});
}
cin >> m;
queue<pair<int , int> > b;
for (int i = 1; i <= m; i++){
int x , y;
cin >> x >> y;
if (x == 0) continue;
b.push({x , y});
}
while(!b.empty()){
queue<pair<int , int> > qu;
qu.push({b.front().first , b.front().second});
b.pop();
vector<pair<int , int>> in;
while(!qu.empty()){
int x = qu.front().first , y = qu.front().second;
qu.pop();
vector<pair<int , int>> in1;
while(x){
if (a.size() == 0){
cout << 0;
return;
}
pair<int , int> p = *a.begin();
a.erase(a.find(p));
if (p.first >= y){
if (p.first - y == 0){
if (p.second > x){
in1.pb({p.first , p.second - x});
}
}
else {
if (p.second > x){
in1.pb({p.first , p.second - x});
}
in.pb({p.first - y , min(p.second , x)});
}
x -= min(x , p.second);
}
else {
int x1 = 0 , y1 = 0;
y1 = y - p.first;
x1 = x;
qu.push({x1 , y1});
y -= p.first;
if (p.first - y == 0){
if (p.second > x){
in1.pb({p.first , p.second - x});
}
}
else {
if (p.second > x){
in1.pb({p.first , p.second - x});
}
in.pb({p.first - y , min(p.second , x)});
}
x -= min(x , p.second);
}
}
for (auto to : in1){
a.insert(to);
}
}
for (auto to : in){
a.insert(to);
}
}
if (a.size()){
cout << 0;
return;
}
cout << 1;
}
signed main(){
PRaim_bek_abi
int t=1;
//cin>>t;
for (int respagold = 1; respagold <= t; respagold++) arkanefury228();
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |