#include<iostream>
#include<algorithm>
using namespace std;
struct star{
int d,m;
}s[300010];
bool cmp(star a,star b){
if(a.d==b.d) return a.m<a.m;
return a.d<b.d;
}
int main(){
int tot=1,n,k,ans=0,nowk;
cin>>n>>k;
for(int i=1;i<=n;i++) cin>>s[i].d>>s[i].m;
sort(s+1,s+1+n,cmp);
int maxn=s[n].d;
nowk=k;
for(int i=1;i<=maxn+1;i++){
while(s[tot].d+1!=i&&s[tot].d!=i&&tot<=n) tot++;
if(tot==n+1)break;
if(s[tot].m>nowk){
ans+=nowk;
s[tot].m-=nowk;
nowk=k;
continue;
}
while(s[tot].m<=nowk&&(s[tot].d==i||s[tot].d+1==i)){
nowk-=s[tot].m;
ans+=s[tot].m;
tot++;
}
nowk=k;
}
cout<<ans<<endl;
return 0;
}
可以吗?