題目鏈接:https://ac.nowcoder.com/acm/problem/50439
思路:思路:我們考慮按s[i]存入vectoc,從大到小枚舉s[i]的值。那麼就是在所有>=s[i]的士兵中選v最大的s[i]個。我們可以優先隊列維護。因爲s[i]是減小的。所以刪除的士兵一定在後面用不到。
#include <bits/stdc++.h>
using namespace std;
#define LL long long
priority_queue<LL, vector<LL>, greater<LL> > q;
vector<LL> ve[100005];
int main(){
int n; scanf("%d", &n);
for(int i=1; i<=n; i++){
LL v, s;
scanf("%lld%lld", &v, &s);
ve[s].push_back(v);
}
LL ans=0, sum=0;
for(int i=n; i>=1; i--){
for(auto u: ve[i]){
sum+=u;
q.push(u);
}
while(q.size()>i){
sum-=q.top();
q.pop();
}
ans=max(ans, sum);
}
printf("%lld\n", ans);
return 0;
}