#include "robots.h"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <set>
#include <queue>
#define endl '\n'
using namespace std;
pair<int,int> toys[2000000]; // toys weight,size
int y[2000000]; // robots size
int x[2000000]; // robots weight
int a,b,t;
bool check(int mid) {
// by biggest size
priority_queue<pair<int,int>> q;
int ptr=0;
for(int i = 0; i < a; i++) {
int br=1;
while(ptr<t && x[i]>toys[ptr].first) {
q.push({toys[ptr].second, toys[ptr].first});
ptr++;
}
br=0;
while(!q.empty() && br<mid) {
q.pop();
br++;
}
}
while(ptr<t) {
q.push({toys[ptr].second, toys[ptr].first});
ptr++;
}
vector<int> v;
while(!q.empty()) {
v.push_back(q.top().first);
q.pop();
}
reverse(v.begin(), v.end());
ptr=0;
for(int i = 0; i < b; i++) {
int br=1;
if(y[i]<=v[ptr]) continue;
while(ptr<v.size() && y[i]>v[ptr] && br<=mid) {ptr++; br++;}
}
return ptr==v.size();
}
int putaway(int a, int b, int t, int x[], int y[], int w[], int s[]) {
for(int i = 0; i < t; i++) toys[i]={w[i], s[i]};
for(int i = 0; i < a; i++) ::x[i]=x[i];
for(int i = 0; i < b; i++) ::y[i]=y[i];
sort(toys, toys+t);
sort(::x, ::x+a);
sort(::y, ::y+b);
::a=a;
::b=b;
::t=t;
int l = 1, r = t, mid, ans=-1;
while(l<=r) {
mid=(l+r)/2;
if(check(mid)) {
ans = mid;
r = mid - 1;
}
else {
l = mid + 1;
}
}
return ans;
}