题目传送门
与(color{red}{这个题})思路差不多,本题可以先忽略距离的影响,最后再考虑.
一定一定一定一定一定一定一定一定一定一定要开long long
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
int n,k1,ans;
long long k,m;
struct kkk {
long long x,sum;
}e[100001];
priority_queue<kkk> q;
inline bool operator <(kkk a,kkk b) {
return a.sum < b.sum;
}
inline bool cmp(kkk a,kkk b) {
return a.x < b.x;
}
int main() {
scanf("%d%lld",&n,&m);
for(int i = 1;i <= n; i++)
scanf("%lld%lld",&e[i].x,&e[i].sum);
sort(e+1,e+n+1,cmp);
for(int i = 1;i <= n; i++) {
if(k + e[i].x + e[i].sum <= m) {
k = k + e[i].sum;
ans++;
q.push(e[i]);
}
else {
if(q.empty()) continue;
kkk o = q.top();
if(k - o.sum + e[i].x + e[i].sum < k) {
k = k - o.sum + e[i].x + e[i].sum;
q.pop();
q.push(e[i]);
}
}
}
printf("%d",ans);
return 0;
}