Source Code
#include <iostream>
#include <vector>
#include <algorithm>
#define f first
#define s second
using namespace std;

pair < long long, long long>a[1000001];
void solve() {
	long long n, m;
	cin >> n >> m;
	for (int i = 0; i < n; i++) {
		cin >> a[i].f;
	}
	for (int i = 0; i < n; i++) {
		cin >> a[i].s;
	}
	sort(a, a + n);
	int i = 0;
	int j = 0;
	for (i; i < n; i++) {
		while (a[i + j].f == a[i + 1 + j].f) {
			j++;
		}
		while (j--) {
			m -= a[i + j + 1].f;
			if (m <= 0) {
				break;
			}
			m += a[i + j + 1].s;
		}
		j++;
		m -= a[i].f;
		if (m <= 0) {
			break;
		}
		m += a[i].s;
	}
	cout << i;
}

int main() {
	int t = 1;
	//cin >> t;
	while(t--){
		solve();
	}
}
Copy
Wheatly's portals - Hard version lafi-Odeh
GNU G++17
2 ms
820 KB
Wrong Answer