题目描述:
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std ;
#define MAXN 200005
#define LL long long
inline void read(LL &x){
x=0 ; int f=1 ; char c=getchar();
while (c>'9' ||c<'0' ){ if (c=='-' ) f=-1 ; c=getchar(); }
while (c>='0' &&c<='9' ){ x=x*10 +c-'0' ; c=getchar(); } x*=f;
}
struct Data{ int id; LL val;bool used; }da[MAXN];
LL sum[MAXN];
inline bool cmp(Data a,Data b){
if (a.id==b.id) return a.val < b.val;
return a.id < b.id;
}
inline bool CMP(Data a,Data b){
return a.val < b.val;
}
int Main(){
int n,m,s,k,tot=0 ;
LL ei,ti;
scanf ("%d%d%d%d" ,&n,&m,&s,&k);
register int i;
for (i=1 ; i<=m; ++i){
scanf ("%lld%lld" ,&ei,&ti);
if (2 -ti < 0 ) da[++tot].id=ei,da[tot].val=2 -ti;
}
sort(da+1 ,da+tot+1 ,cmp);
int num=1 ,sum=0 ,flag=0 ,cnt=0 ;
LL ans=0 ;
for (i=1 ;i<=tot;++i){
if (num>da[i].id) continue ;
if (num<da[i].id) { flag=1 ;break ; }
if (da[i].val<0 &&sum<k){
sum-=da[i].val,++cnt,da[i].used=1 ;
if (cnt>s){ flag=1 ; break ; }
if (sum>=k){ ++num,ans+=sum,sum=0 ; }
}
}
if (flag){ printf ("-23333333\n" ); return 0 ; }
sort(da+1 ,da+tot+1 ,CMP);
for (int i=1 ;i<=tot;++i)
if (da[i].used) continue ;
else if (da[i].val<0 ){
++cnt,ans-=da[i].val;
if (cnt==s) break ;
}
printf ("%lld\n" ,ans);
return 0 ;
}
int Aptal_is_My_Son=Main();
int main(int argc,char *argv[]){ ; }