stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp(a, b) make_pair(a, b)

#define pb(t) push_back(t)

#define ft first

#define sc second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;









int main(void) {

    ll N, C;

    cin >> N >> C;

    ll x[MAX], v[MAX];

    ll s[MAX]={};

    ll r[MAX]={};

    ll si[MAX]={};

    ll ssi[MAX]={};

    ll i;

    

    x[0]=0; v[0]=0;

    for(i=1; i<=N; i++) cin >> x[i] >> v[i];

    x[N+1]=C; v[N+1]=0;

    

    for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

    for(i=N; i>=1; i--) r[i]=r[i+1]+v[i];

    

    ll mx=0, mi=0;

    for(i=1; i<=N; i++) {

        if(s[i]-x[i]>mx) {

            mx=s[i]-x[i];

            mi=i;

        }

        si[i]=mi;

    }

    

    mx=0; mi=0;

    for(i=1; i<=N; i++) {

        if(s[i]-x[i]*2>mx) {

            mx=s[i]-x[i]*2;

            mi=i;

        }

        ssi[i]=mi;

    }

    

    ll ans=0;

    for(ll b=1; b<=N+1; b++) {

        chmax(ans, s[ssi[b-1]]-x[ssi[b-1]]*2+r[b]-(C-x[b]));

        chmax(ans, s[si[b-1]]-x[si[b-1]]+r[b]-(C-x[b])*2);

    }

    pt(ans);

}








// Faster C++ solution for the same problem (measured speedup: 1.6134063901461135x):
#include <bits/stdc++.h>

#define ft first

#define sc second

#define lb lower_bound

#define ub upper_bound

#define pb push_back

#define pt(sth) cout << sth << "\n"

#define chmax(a, b) {if(a<b) a=b;}

#define chmin(a, b) {if(a>b) a=b;}

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

static const ll INF=1e18;

static const ll MAX=1e5+7;

static const ll MOD=1e9+7;

ll max(ll a, ll b) {return a>b?a:b;}

ll min(ll a, ll b) {return a<b?a:b;}







int main(void) {

  ll N, C;

  cin >> N >> C;

  ll i;

  ll x[MAX], v[MAX];

  ll s[MAX]={}, r[MAX]={};

  ll si[MAX], ssi[MAX];

  

  x[0]=0; v[0]=0; x[N+1]=C; v[N+1]=0;

  for(i=1; i<=N; i++) cin >> x[i] >> v[i];

  

  for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

  for(i=N; i>=1; i--) r[i]=r[i+1]+v[i];

  

  ll mx=0, id=0;

  for(i=1; i<=N; i++) {

    if(mx<s[i]-x[i]) {

      mx=s[i]-x[i];

      id=i;

    }

    si[i]=id;

  }

  

  mx=0; id=0;

  for(i=1; i<=N; i++) {

    if(mx<s[i]-x[i]*2) {

      mx=s[i]-x[i]*2;

      id=i;

    }

    ssi[i]=id;

  }

  

  ll ans=0;

  for(i=1; i<=N+1; i++) {

    chmax(ans, s[si[i-1]]-x[si[i-1]]+r[i]-(C-x[i])*2);

    chmax(ans, s[ssi[i-1]]-x[ssi[i-1]]*2+r[i]-(C-x[i]));

  }

  

  pt(ans);

}
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp(a, b) make_pair(a, b)

#define pb(t) push_back(t)

#define ft first

#define sc second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;









int main(void) {

    ll N, C;

    cin >> N >> C;

    ll x[MAX], v[MAX];

    ll s[MAX]={};

    ll r[MAX]={};

    ll si[MAX]={};

    ll ssi[MAX]={};

    ll i;

    

    x[0]=0; v[0]=0;

    for(i=1; i<=N; i++) cin >> x[i] >> v[i];

    x[N+1]=C; v[N+1]=0;

    

    for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

    for(i=N; i>=1; i--) r[i]=r[i+1]+v[i];

    

    ll mx=0, mi=0;

    for(i=1; i<=N; i++) {

        if(s[i]-x[i]>mx) {

            mx=s[i]-x[i];

            mi=i;

        }

        si[i]=mi;

    }

    

    mx=0; mi=0;

    for(i=1; i<=N; i++) {

        if(s[i]-x[i]*2>mx) {

            mx=s[i]-x[i]*2;

            mi=i;

        }

        ssi[i]=mi;

    }

    

    ll ans=0;

    for(ll b=1; b<=N+1; b++) {

        chmax(ans, s[ssi[b-1]]-x[ssi[b-1]]*2+r[b]-(C-x[b]));

        chmax(ans, s[si[b-1]]-x[si[b-1]]+r[b]-(C-x[b])*2);

    }

    pt(ans);

}








// Faster C++ solution for the same problem (measured speedup: 1.5997937446111554x):
#include <bits/stdc++.h>

#include <string>

#define pt(sth) cout << sth << "\n"

#define chmax(a, b) {if(a<b) a=b;}

#define chmin(a, b) {if(a>b) a=b;}

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

using namespace std;

typedef long long ll;

typedef pair<ll, ll> PL;

static const ll INF=1e18;

static const ll MAX=101010;

static const ll MOD=1e9+7;



/*

 for(i=0; i<N; i++)

   cin >> a[i];

*/







int main(void) {

  ll i, j, k;

  

  ll N, C;

  cin >> N >> C;

  ll x[MAX], v[MAX];

  ll s[MAX]={}, t[MAX]={};

  

  x[0]=0; v[0]=0;

  x[N+1]=C; v[N+1]=0;

  for(i=1; i<=N; i++) cin >> x[i] >> v[i];

  for(i=1; i<=N+1; i++) s[i]=s[i-1]+v[i];

  for(i=N; i>=0; i--) t[i]=t[i+1]+v[i];

  

  ll p[MAX], pp[MAX];

  ll m=-INF, mm=-INF;

  ll d=0, dd=0;

  for(i=0; i<=N; i++) {

    if(m<s[i]-x[i]) {m=s[i]-x[i]; d=i;}

    if(mm<s[i]-x[i]*2) {mm=s[i]-x[i]*2; dd=i;}

    p[i]=d; pp[i]=dd;

  }

  

  ll ans=0;

  for(i=1; i<=N+1; i++) {

    ll sco, po;

    sco=0;

    po=p[i-1];

    sco+=s[po]-x[po]+t[i]-(C-x[i])*2; //OA+OB*2

    chmax(ans, sco);

    

    sco=0;

    po=pp[i-1];

    sco+=s[po]-x[po]*2+t[i]-(C-x[i]); //OA*2+OB

    chmax(ans, sco);

  }

  

  pt(ans);

}
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp(a, b) make_pair(a, b)

#define pb(t) push_back(t)

#define ft first

#define sc second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;









int main(void) {

    ll N, C;

    cin >> N >> C;

    ll x[MAX], v[MAX];

    ll s[MAX]={};   //先頭からのカロリーの累積和

    ll r[MAX]={};   //後ろからのカロリーの累積和

    ll si[MAX]={};  //si[i]:i番目まででs[k]-x[k]が最大となるような位置k (k<=i)

    ll ssi[MAX]={}; //ssi[i]:i番目まででs[k]-x[k]*2が最大となるような位置k (k<=i)

    ll i;

    

    x[0]=0; v[0]=0;

    for(i=1; i<=N; i++) cin >> x[i] >> v[i];

    x[N+1]=C; v[N+1]=0;

    

    for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

    for(i=N; i>=1; i--) r[i]=r[i+1]+v[i];

    

    ll mx=0, mi=0;

    for(i=1; i<=N; i++) {       //si[i]を求める

        if(s[i]-x[i]>mx) {

            mx=s[i]-x[i];

            mi=i;

        }

        si[i]=mi;

    }

    

    mx=0; mi=0;

    for(i=1; i<=N; i++) {       //ssi[i]を求める

        if(s[i]-x[i]*2>mx) {

            mx=s[i]-x[i]*2;

            mi=i;

        }

        ssi[i]=mi;

    }

    

    ll ans=0;

    for(ll b=1; b<=N+1; b++) {

        chmax(ans, s[si[b-1]]-x[si[b-1]]+r[b]-(C-x[b])*2);  //OA+2OBの場合

        chmax(ans, s[ssi[b-1]]-x[ssi[b-1]]*2+r[b]-(C-x[b]));//2OA+OBの場合

    }

    pt(ans);

}








// Faster C++ solution for the same problem (measured speedup: 1.5998039907804968x):
#include <bits/stdc++.h>

#include <string>

#define pt(sth) cout << sth << "\n"

#define chmax(a, b) {if(a<b) a=b;}

#define chmin(a, b) {if(a>b) a=b;}

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

using namespace std;

typedef long long ll;

typedef pair<ll, ll> PL;

static const ll INF=1e18;

static const ll MAX=101010;

static const ll MOD=1e9+7;



/*

 for(i=0; i<N; i++)

   cin >> a[i];

*/







int main(void) {

  ll i, j, k;

  

  ll N, C;

  cin >> N >> C;

  ll x[MAX], v[MAX];

  ll s[MAX]={}, t[MAX]={};

  

  x[0]=0; v[0]=0;

  x[N+1]=C; v[N+1]=0;

  for(i=1; i<=N; i++) cin >> x[i] >> v[i];

  for(i=1; i<=N+1; i++) s[i]=s[i-1]+v[i];

  for(i=N; i>=0; i--) t[i]=t[i+1]+v[i];

  

  ll p[MAX], pp[MAX];

  ll m=-INF, mm=-INF;

  ll d=0, dd=0;

  for(i=0; i<=N; i++) {

    if(m<s[i]-x[i]) {m=s[i]-x[i]; d=i;}

    if(mm<s[i]-x[i]*2) {mm=s[i]-x[i]*2; dd=i;}

    p[i]=d; pp[i]=dd;

  }

  

  ll ans=0;

  for(i=1; i<=N+1; i++) {

    ll sco, po;

    sco=0;

    po=p[i-1];

    sco+=s[po]-x[po]+t[i]-(C-x[i])*2; //OA+OB*2

    chmax(ans, sco);

    

    sco=0;

    po=pp[i-1];

    sco+=s[po]-x[po]*2+t[i]-(C-x[i]); //OA*2+OB

    chmax(ans, sco);

  }

  

  pt(ans);

}
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp(a, b) make_pair(a, b)

#define pb(t) push_back(t)

#define ft first

#define sc second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;









int main(void) {

    ll N, C;

    cin >> N >> C;

    ll x[MAX], v[MAX];

    ll s[MAX]={};

    ll r[MAX]={};

    ll si[MAX]={};

    ll ssi[MAX]={};

    ll i;

    

    x[0]=0; v[0]=0;

    for(i=1; i<=N; i++) cin >> x[i] >> v[i];

    x[N+1]=C; v[N+1]=0;

    

    for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

    for(i=N; i>=1; i--) r[i]=r[i+1]+v[i];

    

    ll mx=0, mi=0;

    for(i=1; i<=N; i++) {

        if(s[i]-x[i]>mx) {

            mx=s[i]-x[i];

            mi=i;

        }

        si[i]=mi;

    }

    

    mx=0; mi=0;

    for(i=1; i<=N; i++) {

        if(s[i]-x[i]*2>mx) {

            mx=s[i]-x[i]*2;

            mi=i;

        }

        ssi[i]=mi;

    }

    

    ll ans=0;

    for(ll b=1; b<=N+1; b++) {

        chmax(ans, s[ssi[b-1]]-x[ssi[b-1]]*2+r[b]-(C-x[b]));

        chmax(ans, s[si[b-1]]-x[si[b-1]]+r[b]-(C-x[b])*2);

    }

    pt(ans);

}








// Faster C++ solution for the same problem (measured speedup: 1.5997512910855365x):
#include <bits/stdc++.h>

#include <string>

#define pt(sth) cout << sth << "\n"

#define chmax(a, b) {if(a<b) a=b;}

#define chmin(a, b) {if(a>b) a=b;}

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

using namespace std;

typedef long long ll;

typedef pair<ll, ll> PL;

static const ll INF=1e18;

static const ll MAX=101010;

static const ll MOD=1e9+7;



/*

 for(i=0; i<N; i++)

   cin >> a[i];

*/







int main(void) {

  ll i, j, k;

  

  ll N, C;

  cin >> N >> C;

  ll x[MAX], v[MAX];

  ll s[MAX]={}, t[MAX]={};

  

  x[0]=0; v[0]=0;

  x[N+1]=C; v[N+1]=0;

  for(i=1; i<=N; i++) cin >> x[i] >> v[i];

  for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

  for(i=N; i>=1; i--) t[i]=t[i+1]+v[i];

  

  ll p[MAX], pp[MAX];

  ll m=-INF, mm=-INF;

  ll d=0, dd=0;

  for(i=0; i<=N; i++) {

    if(m<s[i]-x[i]) {m=s[i]-x[i]; d=i;}

    if(mm<s[i]-x[i]*2) {mm=s[i]-x[i]*2; dd=i;}

    p[i]=d; pp[i]=dd;

  }

  

  ll ans=0;

  for(i=1; i<=N+1; i++) {

    chmax(ans, s[p[i-1]]-x[p[i-1]]+t[i]-(C-x[i])*2); //OA+OB*2

    chmax(ans, s[pp[i-1]]-x[pp[i-1]]*2+t[i]-(C-x[i])); //OA*2+OB

  }

  

  pt(ans);

}
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp(a, b) make_pair(a, b)

#define pb(t) push_back(t)

#define ft first

#define sc second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;









int main(void) {

    ll N, C;

    cin >> N >> C;

    ll x[MAX], v[MAX];

    ll s[MAX]={};   //先頭からのカロリーの累積和

    ll r[MAX]={};   //後ろからのカロリーの累積和

    ll si[MAX]={};  //si[i]:i番目まででs[k]-x[k]が最大となるような位置k (k<=i)

    ll ssi[MAX]={}; //ssi[i]:i番目まででs[k]-x[k]*2が最大となるような位置k (k<=i)

    ll i;

    

    x[0]=0; v[0]=0;

    for(i=1; i<=N; i++) cin >> x[i] >> v[i];

    x[N+1]=C; v[N+1]=0;

    

    for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

    for(i=N; i>=1; i--) r[i]=r[i+1]+v[i];

    

    ll mx=0, mi=0;

    for(i=1; i<=N; i++) {       //si[i]を求める

        if(s[i]-x[i]>mx) {

            mx=s[i]-x[i];

            mi=i;

        }

        si[i]=mi;

    }

    

    mx=0; mi=0;

    for(i=1; i<=N; i++) {       //ssi[i]を求める

        if(s[i]-x[i]*2>mx) {

            mx=s[i]-x[i]*2;

            mi=i;

        }

        ssi[i]=mi;

    }

    

    ll ans=0;

    for(ll b=1; b<=N+1; b++) {

        chmax(ans, s[si[b-1]]-x[si[b-1]]+r[b]-(C-x[b])*2);  //OA+2OBの場合

        chmax(ans, s[ssi[b-1]]-x[ssi[b-1]]*2+r[b]-(C-x[b]));//2OA+OBの場合

    }

    pt(ans);

}








// Faster C++ solution for the same problem (measured speedup: 1.5997615369829767x):
#include <bits/stdc++.h>

#include <string>

#define pt(sth) cout << sth << "\n"

#define chmax(a, b) {if(a<b) a=b;}

#define chmin(a, b) {if(a>b) a=b;}

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

using namespace std;

typedef long long ll;

typedef pair<ll, ll> PL;

static const ll INF=1e18;

static const ll MAX=101010;

static const ll MOD=1e9+7;



/*

 for(i=0; i<N; i++)

   cin >> a[i];

*/







int main(void) {

  ll i, j, k;

  

  ll N, C;

  cin >> N >> C;

  ll x[MAX], v[MAX];

  ll s[MAX]={}, t[MAX]={};

  

  x[0]=0; v[0]=0;

  x[N+1]=C; v[N+1]=0;

  for(i=1; i<=N; i++) cin >> x[i] >> v[i];

  for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

  for(i=N; i>=1; i--) t[i]=t[i+1]+v[i];

  

  ll p[MAX], pp[MAX];

  ll m=-INF, mm=-INF;

  ll d=0, dd=0;

  for(i=0; i<=N; i++) {

    if(m<s[i]-x[i]) {m=s[i]-x[i]; d=i;}

    if(mm<s[i]-x[i]*2) {mm=s[i]-x[i]*2; dd=i;}

    p[i]=d; pp[i]=dd;

  }

  

  ll ans=0;

  for(i=1; i<=N+1; i++) {

    chmax(ans, s[p[i-1]]-x[p[i-1]]+t[i]-(C-x[i])*2); //OA+OB*2

    chmax(ans, s[pp[i-1]]-x[pp[i-1]]*2+t[i]-(C-x[i])); //OA*2+OB

  }

  

  pt(ans);

}
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp(a, b) make_pair(a, b)

#define pb(t) push_back(t)

#define ft first

#define sc second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;









int main(void) {

    ll N, C;

    cin >> N >> C;

    ll x[MAX], v[MAX];

    ll s[MAX]={};

    ll r[MAX]={};

    ll si[MAX]={};

    ll ssi[MAX]={};

    ll i;

    

    x[0]=0; v[0]=0;

    for(i=1; i<=N; i++) cin >> x[i] >> v[i];

    x[N+1]=C; v[N+1]=0;

    

    for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

    for(i=N; i>=1; i--) r[i]=r[i+1]+v[i];

    

    ll mx=0, mi=0;

    for(i=1; i<=N; i++) {

        if(s[i]-x[i]>mx) {

            mx=s[i]-x[i];

            mi=i;

        }

        si[i]=mi;

    }

    

    mx=0; mi=0;

    for(i=1; i<=N; i++) {

        if(s[i]-x[i]*2>mx) {

            mx=s[i]-x[i]*2;

            mi=i;

        }

        ssi[i]=mi;

    }

    

    ll ans=0;

    for(ll b=1; b<=N+1; b++) {

        chmax(ans, s[ssi[b-1]]-x[ssi[b-1]]*2+r[b]-(C-x[b]));

        chmax(ans, s[si[b-1]]-x[si[b-1]]+r[b]-(C-x[b])*2);

    }

    pt(ans);

}








// Faster C++ solution for the same problem (measured speedup: 1.624016686196859x):
#include <bits/stdc++.h>

#include <string>

#define pt(sth) cout << sth << "\n"

#define chmax(a, b) {if(a<b) a=b;}

#define chmin(a, b) {if(a>b) a=b;}

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

using namespace std;

typedef long long ll;

typedef pair<ll, ll> PL;

static const ll INF=1e18;

static const ll MAX=101010;

static const ll MOD=1e9+7;



/*

 for(i=0; i<N; i++)

   cin >> a[i];

*/







int main(void) {

  ll i, j, k;

  

  ll N, C;

  cin >> N >> C;

  ll x[MAX], v[MAX];

  ll s[MAX]={}, t[MAX]={};

  

  x[0]=0; v[0]=0;

  x[N+1]=C; v[N+1]=0;

  for(i=1; i<=N; i++) cin >> x[i] >> v[i];

  for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

  for(i=N; i>=1; i--) t[i]=t[i+1]+v[i];

  

  ll ans=0;

  ll m=-INF, mm=-INF;

  for(i=0; i<=N; i++) {

    chmax(m, s[i]-x[i]);

    chmax(mm, s[i]-x[i]*2);

    

    chmax(ans, m+t[i+1]-(C-x[i+1])*2); //OA+OB*2

    chmax(ans, mm+t[i+1]-(C-x[i+1])); //OA*2+OB

  }

  

  pt(ans);

}
// Slower C++ solution:
#include <iostream>

#include <cmath>

#include <string>

#include <algorithm>

#include <set>

#include <vector>

#include <map>

#include <list>

#include <stack>

#include <cstdio>

#include <cstring>

#include <cstdlib>

#include <queue>

#define mkp(a, b) make_pair(a, b)

#define pb(t) push_back(t)

#define ft first

#define sc second

#define pt(num) cout << num << "\n"

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

#define max(a, b) ((a)>(b) ? (a):(b))

#define min(a, b) ((a)<(b) ? (a):(b))

#define chmax(a, b) (a<b ? a=b : 0)

#define chmin(a, b) (a>b ? a=b : 0)

#define INF 1000000000000000000

#define MOD 1000000007LL

#define MAX 101010

using namespace std;

typedef long long ll;

typedef pair<ll, ll> P;

typedef map<ll, ll> Map;









int main(void) {

    ll N, C;

    cin >> N >> C;

    ll x[MAX], v[MAX];

    ll s[MAX]={};   //先頭からのカロリーの累積和

    ll r[MAX]={};   //後ろからのカロリーの累積和

    ll si[MAX]={};  //si[i]:i番目まででs[k]-x[k]が最大となるような位置k (k<=i)

    ll ssi[MAX]={}; //ssi[i]:i番目まででs[k]-x[k]*2が最大となるような位置k (k<=i)

    ll i;

    

    x[0]=0; v[0]=0;

    for(i=1; i<=N; i++) cin >> x[i] >> v[i];

    x[N+1]=C; v[N+1]=0;

    

    for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

    for(i=N; i>=1; i--) r[i]=r[i+1]+v[i];

    

    ll mx=0, mi=0;

    for(i=1; i<=N; i++) {       //si[i]を求める

        if(s[i]-x[i]>mx) {

            mx=s[i]-x[i];

            mi=i;

        }

        si[i]=mi;

    }

    

    mx=0; mi=0;

    for(i=1; i<=N; i++) {       //ssi[i]を求める

        if(s[i]-x[i]*2>mx) {

            mx=s[i]-x[i]*2;

            mi=i;

        }

        ssi[i]=mi;

    }

    

    ll ans=0;

    for(ll b=1; b<=N+1; b++) {

        chmax(ans, s[si[b-1]]-x[si[b-1]]+r[b]-(C-x[b])*2);  //OA+2OBの場合

        chmax(ans, s[ssi[b-1]]-x[ssi[b-1]]*2+r[b]-(C-x[b]));//2OA+OBの場合

    }

    pt(ans);

}








// Faster C++ solution for the same problem (measured speedup: 1.6240270875064253x):
#include <bits/stdc++.h>

#include <string>

#define pt(sth) cout << sth << "\n"

#define chmax(a, b) {if(a<b) a=b;}

#define chmin(a, b) {if(a>b) a=b;}

#define moC(a, s, b) (a)=((a)s(b)+MOD)%MOD

using namespace std;

typedef long long ll;

typedef pair<ll, ll> PL;

static const ll INF=1e18;

static const ll MAX=101010;

static const ll MOD=1e9+7;



/*

 for(i=0; i<N; i++)

   cin >> a[i];

*/







int main(void) {

  ll i, j, k;

  

  ll N, C;

  cin >> N >> C;

  ll x[MAX], v[MAX];

  ll s[MAX]={}, t[MAX]={};

  

  x[0]=0; v[0]=0;

  x[N+1]=C; v[N+1]=0;

  for(i=1; i<=N; i++) cin >> x[i] >> v[i];

  for(i=1; i<=N; i++) s[i]=s[i-1]+v[i];

  for(i=N; i>=1; i--) t[i]=t[i+1]+v[i];

  

  ll ans=0;

  ll m=-INF, mm=-INF;

  for(i=0; i<=N; i++) {

    chmax(m, s[i]-x[i]);

    chmax(mm, s[i]-x[i]*2);

    

    chmax(ans, m+t[i+1]-(C-x[i+1])*2); //OA+OB*2

    chmax(ans, mm+t[i+1]-(C-x[i+1])); //OA*2+OB

  }

  

  pt(ans);

}
// Slower C++ solution:
#include<iostream>

#include<cstdio>

#include<cstring>

#include<algorithm>

#define MAXN (101010)

#define LL long long

using namespace std;

int n; 

struct Node{

	LL p,v;

	bool operator < (const Node &a)const{

		return p<a.p;

	}

	void Read(){

		scanf("%lld %lld",&p,&v);

	}

}T[MAXN];

LL C,A1[MAXN],A2[MAXN],B1[MAXN],B2[MAXN],p[MAXN],v[MAXN];

LL Max(LL a,LL b){

	if (a>b) return a;

	return b;

}

int main(){

	scanf("%d %lld",&n,&C);

	int i;

	LL ans=0;

	for (i=1;i<=n;i++) 

		T[i].Read();

	sort(T+1,T+n+1);

	for (i=1;i<=n;i++) p[i]=T[i].p,v[i]=T[i].v;

	p[n+1]=C;

	for (i=1;i<=n;i++){

		A1[i]=A1[i-1]+v[i]-(p[i]-p[i-1]);

		ans=Max(A1[i],ans);

		B1[i]=A1[i]-p[i];

		if (i>1) B1[i]=Max(B1[i],B1[i-1]);

	}

	for (i=n;i>=1;i--){

		A2[i]=A2[i+1]+v[i]-(p[i+1]-p[i]);

		ans=Max(ans,A2[i]);

		B2[i]=A2[i]-(C-p[i]);

		if (i<n) B2[i]=Max(B2[i],B2[i+1]);

	}

	for (i=1;i<n;i++) ans=Max(ans,A1[i]+B2[i+1]);

	for (i=2;i<=n;i++) ans=Max(ans,A2[i]+B1[i-1]);

	cout<<ans;

	

}

// Faster C++ solution for the same problem (measured speedup: 5.164976230054643x):
#include<cstdio>

#define MAXN 100010

#define ll long long

#define max(a,b) (a>b?a:b)

#define INF 0x3f3f3f3f3f3f3f3f

using namespace std;



int n;

ll c,x[MAXN],v[MAXN];

ll k[5][MAXN],ma[5][MAXN];



int main()

{

	scanf("%d%lld",&n,&c);

	for(register int i=1;i<=n;i++)

		scanf("%lld%lld",&x[i],&v[i]);

	x[n+1]=c;  x[0]=0LL;

	long long sum=0LL,ans=-1e18;

	for(register int i=1;i<=n;i++)

	{

		sum=sum-(x[i]-x[i-1])+v[i];

		ans=max(ans,sum);

	}

	sum=0LL;

	for(register int i=n;i>=1;i--)

	{

		sum=sum-(x[i+1]-x[i])+v[i];

		ans=max(ans,sum);

	}

	for(register int i=1;i<=n;i++)

	{

		k[1][i]=k[1][i-1]+v[i]+2*x[i-1]-2*x[i];

		k[2][i]=k[2][i-1]+v[i]+x[i-1]-x[i];

	}

	for(register int i=n;i>=1;i--)

	{

		k[3][i]=k[3][i+1]+v[i]+2*(c-x[i+1])-2*(c-x[i]);

		k[4][i]=k[4][i+1]+v[i]+(c-x[i+1])-(c-x[i]);

	}

	//  1: 顺 - 先到i    v[1]+v[2]+...+v[i] - 2x[i] 

	//  2: 顺 - 后到i    v[1]+v[2]+...+v[i] - x[i]

	//  3: 逆 - 先到i    v[n]+v[n-1]+...+v[i] - 2(c-x[i])

	//  4: 逆 - 后到i    v[n]+v[n-1]+...+v[i] - (c-x[i])

	

	//  1: 逆 - 后到 i~n 的最大值

	//  2: 逆 - 先到 i~n 的最大值

	//  3: 顺 - 后到 1~i 的最大值

	//  4: 顺 - 先到 1~i 的最大值

	for(register int i=n;i>=1;i--)

	{

		ma[1][i]=max(ma[1][i+1],k[4][i]);

		ma[2][i]=max(ma[2][i+1],k[3][i]);

	}

	for(register int i=1;i<=n;i++)

	{

		ma[3][i]=max(ma[3][i-1],k[2][i]);

		ma[4][i]=max(ma[4][i-1],k[1][i]);

	}

	for(register int i=1;i<=n;i++)

	{

		sum=k[1][i]+ma[1][i+1];//先顺到 i 再逆到 j  (i<j)

		ans=max(sum,ans);

		sum=k[3][i]+ma[3][i-1];//先逆到 i 再顺到 j  (i>j)

		ans=max(sum,ans);

		sum=k[2][i]+ma[2][i+1];//后顺到 i 先逆到 j  (i<j)

		ans=max(sum,ans);

		sum=k[4][i]+ma[4][i-1];//后逆到 i 先顺到 j  (i>j)

		ans=max(sum,ans);

	}

	if(ans<0LL)  puts("0");

	else  printf("%lld",ans);

	return 0;

}