Source Code
#include <bits/stdc++.h>

using namespace std;

#define all(x) (x).begin(), (x).end()
#define fast ios::sync_with_stdio(false);cin.tie(0);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());

int n,m;
int calc(int k, vector<int> a, vector<int> b){
	vector<int> hv(n+1,0);

	for(int i=0;i<m;i++)hv[b[i]]++;

	int ans = 0;
	int at = 0;
	int old = 0;
	deque<int> q;
	for(int i=n;i>0;i--){
		int bf = hv[i];
		while(q.size()){
			int need = q.front();
			if(need > hv[i]){
				hv[i - 1] += hv[i];
				bf -= hv[i];
				q.front() -= hv[i];
				hv[i] = 0;
				break;
			}else{
				hv[i - 1] += need;
				bf -= need;
				hv[i] -= need;
				q.pop_front();
			}
		}
		hv[i] += old;
		// cerr << i << ' ' << hv[i] << ' ' << at << ' ' << a[at] << ' ' << q.size() << '\n';
		old = bf;
		while(at < n){
			int need = k - a[at];
			if(need <= 0){
				at++;
				continue;
			}
			if(need > hv[i]){
				a[at] += hv[i];
				q.push_back(k - a[at]);
				hv[i] = 0;
				at++;
				break;
			}else{
				a[at] += need;
				hv[i] -= need;
				at++;
			}
		}
	}

	return at - q.size();
}

int main(){
	fast
	cin>>n>>m;
	vector<int> a(n),b(m);

	for(int i=0;i<n;i++)cin>>a[i];
	for(int i=0;i<m;i++)cin>>b[i];

	sort(all(a));
	reverse(all(a));
	sort(all(b));

	ll q;
	cin>>q;
	while(q--){
		ll k;
		cin>>k;

		cout << calc(k , a , b) << ' ';
	}
}

/*
	1- Look at base case (n=1,m=1)
	2- Overflow
	3- Use assert
*/
Copy
Votes Warawreh
GNU G++17
3 ms
476 KB
Wrong Answer