Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- typedef long long ll;
- typedef pair<ll, ll> ii;
- #define fr first
- #define sc second
- #define mp make_pair
- const int MAXN = 50010;
- const ll inf = (1LL << 61);
- int n, k;
- ll budget;
- ii ar[MAXN];
- bool cmp(ii a, ii b)
- {
- if (a.sc == b.sc)
- return a.fr > b.fr;
- return a.sc < b.sc;
- }
- bool delrem[MAXN], delcoup[MAXN], delswapp[MAXN];
- int solve()
- {
- ll cur = 0;
- int ans = 0;
- priority_queue<ii> coup;
- priority_queue<ii, vector<ii>, greater<ii> > rem, swapp;
- for (int i = 0; i < n; i++)
- {
- if (cur + ar[i].sc <= budget)
- {
- coup.push(mp(ar[i].sc, i));
- swapp.push(mp(ar[i].fr - ar[i].sc, i));
- cur += ar[i].sc;
- ans++;
- }
- else
- {
- rem.push(mp(ar[i].fr, i));
- }
- }
- int coupsize = coup.size();
- while (coupsize > k)
- {
- ll a;
- if (rem.empty() || coup.empty())
- a = inf;
- else a = rem.top().fr - coup.top().fr;
- ll b;
- if (swapp.empty())
- b = inf;
- else b = swapp.top().fr;
- if (min(a, b) + cur > budget)
- {
- cur -= coup.top().fr;
- rem.push(mp(ar[coup.top().sc].fr, coup.top().sc));
- delswapp[coup.top().sc] = true;
- ans--;
- coup.pop();
- coupsize--;
- }
- else if (b < a)
- {
- delcoup[swapp.top().sc] = true;
- delrem[swapp.top().sc] = true;
- cur += swapp.top().fr;
- swapp.pop();
- coupsize--;
- }
- else
- {
- delrem[rem.top().sc] = true;
- delswapp[coup.top().sc] = true;
- delswapp[rem.top().sc] = true;
- cur += b;
- rem.push(mp(ar[coup.top().sc].fr, coup.top().sc));
- coup.pop();
- rem.pop();
- coupsize--;
- }
- while (!rem.empty() && delrem[rem.top().sc])
- rem.pop();
- while (!coup.empty() && delcoup[coup.top().sc])
- coup.pop();
- while (!swapp.empty() && delswapp[swapp.top().sc])
- swapp.pop();
- }
- return ans;
- }
- int main()
- {
- scanf("%d%d", &n, &k);
- scanf("%I64d", &budget);
- for (int i = 0; i < n; i++)
- scanf("%I64d%I64d", &ar[i].fr, &ar[i].sc);
- sort(ar, ar + n, cmp);
- printf("%d\n", solve());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement