#include
#include
using namespace std;
int main(){
int n,m,t=0,k=2;
cin>>n>>m;
long long int lon[n+2]={0},num[n+1];
lon[2]=m;
for(int a=0;n>a;a++){
int b,c;
cin>>b>>c;
num[c]=b;
}
for(int a=1;n>=a;a++){
for(int b=1;n>=b;b++)
{
if(num[a]>lon[b]&&lon[b+1]>num[a]){t=t+lon[b+1]-lon[b];k++;
for(int c=k;c>b+1;c--){lon[c]=lon[c-1];}
lon[b+1]=num[a];
break;}
}
}
cout<<t<<endl;
}
歡迎參考我的解法
https://66lemon66.blogspot.com/2021/01/zerojudge-f607-3-c.html