#include "robots.h"
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <unordered_map>
#include <set>
#include <queue>
#define endl '\n'
using namespace std;
int x[2000000];
int w[2000000];
int a,t;
bool check(int mid) {
priority_queue<pair<int, int>, vector<pair<int, int>>, less<pair<int, int>>> q;
for(int i = 1; i <= a; i++) q.push({x[i], 0});
for(int i = 1; i <= t; i++) {
while(!q.empty() && q.top().first<w[i] && q.top().second>=mid) q.pop();
if(q.empty() || q.top().first<w[i] || q.top().second>=mid) return 0;
auto c = q.top(); q.pop();
q.push({c.first-w[i], c.second+1});
}
return 1;
}
int putaway(int a, int b, int t, int x[], int y[], int w[], int s[]) {
if(a+b==2 && t==2) {
if(a==0 && b==2) {
if(s[0]<y[0] && s[1]<y[1]) return 1;
else if(s[0]<y[1] && s[1]<y[0]) return 1;
else if(max(s[0], s[1])<max(y[0], y[1])) return 2;
else return -1;
}
else if(a==1 && b==1) {
if(s[0]<y[0] && w[1]<x[0]) return 1;
else if(w[0]<x[0] && s[1]<y[0]) return 1;
else if(max(s[0], s[1])<y[0]) return 2;
else if(max(w[0], w[1])<x[0]) return 2;
else return -1;
}
else if(a==2 && b==0) {
if(w[0]<x[0] && w[1]<x[1]) return 1;
else if(w[0]<x[1] && w[1]<x[0]) return 1;
else if(max(w[0], w[1])<max(x[0], x[1])) return 2;
else return -1;
}
else return -1;
}
if(b==0) {
for(int i = 0; i < t; i++) ::w[i]=w[i];
for(int i = 0; i < a; i++) ::x[i]=x[i];
sort(w+1, w+1+t);
::a=a;
::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;
}
return -1;
}