为什么我的代码最后的两个sub检测点没过?
#include<bits/stdc++.h>
using namespace std;
const int N=3e5+1;
int n,k;
long long ans=0;
int main(){
cin>>n>>k;
int a[N]={};
int di,mi,max=0;
for(int i=0;i<n;i++){
cin>>di>>mi;
a[di]+=mi;
max=di>max?di:max;
}
long long i,temp=0;
for(i=1;i<=max;i++){
if(temp<=k&&temp>=0){
ans+=temp;
if(a[i]<=k-temp){
ans+=a[i];
temp=0;
}
else{
ans+=k-temp;
temp=a[i]-(k-temp);
}
}
else if(temp>k){
ans+=k;
temp=a[i];
}
}
if(temp<=k&&temp>=0){
ans+=temp;
}
else{
ans+=k;
}
cout<<ans;
return 0;
}