在第8组数据上WA了一天,后来才发现问题;
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #define maxn 200009 #define inf 1e15 using namespace std; int n,k,q; struct node { int score; int pain; bool operator<(const node &t)const { return pain<t.pain; } } no[maxn]; bool cmp(const node &a,const node &b) { return a.score>b.score; } bool vis[maxn]; int s[maxn]; long long check(int p) { long long ans=0; int cnt_1=0; for(int i=1;i<=n;i++) if(no[i].score>=p-1)cnt_1++; cnt_1-=k-1; int cnt_2=0; for(int i=1;i<=n&&cnt_2<cnt_1;i++) { if(no[i].score==p||no[i].score==(p-1)) { ans+=no[i].pain; cnt_2++; vis[i]=1; } } for(int i=1;i<=n&&cnt_2<p;i++) { if(!vis[i]) { ans+=no[i].pain; cnt_2++; } } memset(vis,0,sizeof vis); return ans; } int main() { scanf("%d%d",&n,&k); for(int i=1; i<=n; i++) scanf("%d%d",&no[i].score,&no[i].pain); sort(no+1,no+n+1,cmp); int tar=no[k].score; sort(no+1,no+n+1); long long ans=inf; for(int i=tar;i<=tar+2;i++) { if(i>n)break; ans=min(ans,check(i)); } if(ans==inf)ans=-1; cout<<ans<<endl;; }