#include<bits/stdc++.h>
#include "robots.h"
using namespace std;
vector<pair<int, int>> p;
vector<int> x, y;
int a, b, n;
bool check(int k){
priority_queue<int> pq;
int j = 0;
for(int i = 0; i <a; i++){
while(j<n&&p[j].first<x[i]){
pq.push(p[j].second);
j++;
}
for(int i1 = 0; i1<k; i1++) {
if(pq.size()==0) break;
pq.pop();
}
}
for(int i = j; i<n; i++){
pq.push(p[i].second);
}
for(int i = b-1; i >=0; i--){
for(int i1 = 0; i1<k; i1++) {
if(pq.size()==0) break;
if(y[i]>pq.top()) pq.pop();
else return false;
}
}
if(pq.size()==0) return true;
else return false;
}
int putaway(int A, int B, int N, int X[], int Y[], int w[], int s[]){
for(int i = 0; i < N; i++){
p.push_back({w[i], s[i]});
}
for(int i = 0; i < A ; i++) x.push_back(X[i]);
for(int i = 0; i < B ; i++) y.push_back(Y[i]);
a = A; b = B; n = N;
sort(p.begin(), p.end());
sort(y.begin(), y.end());
sort(x.begin(), x.end());
int l = 0, r = 1000000, ans = 0;
while(l<r){
int m = (l+r)/2;
if(check(m)){
r = m;
ans = m;
} else l = m+1;
}
return ans;
}