#include <bits/stdc++.h>
using namespace std;
#define FOR(i,p,q) for(int i=(p); i<=(q); ++i)
#define ROF(i,p,q) for(int i=(p); i>=(q); --i)
#define REP(i,q) for(int i=0; i<(q); ++i)
#define pb push_back
#define as assign
#define rz resize
#define Co const
#define all(X) X.begin(), X.end()
#define rall(X) X.rbegin(), X.rend()
#define sz(X) (int)(X.size())
#define ckmax(a,b) a=max(a,b)
#define ckmin(a,b) a=min(a,b)
#define V vector
typedef long long ll;
typedef mt19937_64 mt;
#ifndef UNCLE
typedef basic_string<bool> vb;
typedef basic_string<int> vi;
typedef basic_string<ll> vl;
#else
typedef V<bool> vb;
typedef V<int> vi;
typedef V<ll> vl;
#endif
constexpr int MX_C=5e5, base=1<<19;
int N;
ll kar;
struct RngTR{
vl tmx;
void Bu0(){
tmx.as(base<<1,0);
}
void Upt(int v,ll x){
v+=base;
ckmax(tmx[v],x),v/=2;
while(v) tmx[v]=max(tmx[2*v],tmx[2*v+1]), v/=2;
}
ll Mx(int p,int q){
ll pro=0;
p+=base-1,q+=base+1;
while(p/2!=q/2){
if(!(p&1)) ckmax(pro,tmx[p+1]);
if(q&1) ckmax(pro,tmx[q-1]);
p/=2,q/=2;
}
return pro;
}
};
RngTR tr;
struct SS{int h,c; ll rs;};
V<SS> s;
void Input(){
cin>>N>>kar;
tr.Bu0(), s.rz(N);
for(SS &vv:s) cin>>vv.h>>vv.c,--vv.c;
reverse(all(s));
}
void Solve(){
int p=0;
REP(i,N){
while(p<i&&(s[p].h!=s[i].h)) tr.Upt(s[p].c,s[p].rs),++p;
s[i].rs=max(tr.Mx(s[i].c,s[i].c),max(tr.Mx(0,s[i].c-1),tr.Mx(s[i].c+1,MX_C-1))-kar)+s[i].h;
}
while(p<=N-1) tr.Upt(s[p].c,s[p].rs),++p;
cout<<tr.Mx(0,MX_C-1)<<"\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
Input();
Solve();
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | #include <bits/stdc++.h> using namespace std; #define FOR(i,p,q) for(int i=(p); i<=(q); ++i) #define ROF(i,p,q) for(int i=(p); i>=(q); --i) #define REP(i,q) for(int i=0; i<(q); ++i) #define pb push_back #define as assign #define rz resize #define Co const #define all(X) X.begin(), X.end() #define rall(X) X.rbegin(), X.rend() #define sz(X) (int)(X.size()) #define ckmax(a,b) a=max(a,b) #define ckmin(a,b) a=min(a,b) #define V vector typedef long long ll; typedef mt19937_64 mt; #ifndef UNCLE typedef basic_string<bool> vb; typedef basic_string<int> vi; typedef basic_string<ll> vl; #else typedef V<bool> vb; typedef V<int> vi; typedef V<ll> vl; #endif constexpr int MX_C=5e5, base=1<<19; int N; ll kar; struct RngTR{ vl tmx; void Bu0(){ tmx.as(base<<1,0); } void Upt(int v,ll x){ v+=base; ckmax(tmx[v],x),v/=2; while(v) tmx[v]=max(tmx[2*v],tmx[2*v+1]), v/=2; } ll Mx(int p,int q){ ll pro=0; p+=base-1,q+=base+1; while(p/2!=q/2){ if(!(p&1)) ckmax(pro,tmx[p+1]); if(q&1) ckmax(pro,tmx[q-1]); p/=2,q/=2; } return pro; } }; RngTR tr; struct SS{int h,c; ll rs;}; V<SS> s; void Input(){ cin>>N>>kar; tr.Bu0(), s.rz(N); for(SS &vv:s) cin>>vv.h>>vv.c,--vv.c; reverse(all(s)); } void Solve(){ int p=0; REP(i,N){ while(p<i&&(s[p].h!=s[i].h)) tr.Upt(s[p].c,s[p].rs),++p; s[i].rs=max(tr.Mx(s[i].c,s[i].c),max(tr.Mx(0,s[i].c-1),tr.Mx(s[i].c+1,MX_C-1))-kar)+s[i].h; } while(p<=N-1) tr.Upt(s[p].c,s[p].rs),++p; cout<<tr.Mx(0,MX_C-1)<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); Input(); Solve(); } |
English