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

#include<vector>

#include<algorithm>

#include<functional>

using namespace std;

	bool is(int x){

		for(int i=2;i*i<=x;i++)if(x%i==0)return false;

		return true;

	}

int main(){

	int n,p;



	while(cin>>n>>p,n!=-1||p!=-1){

		int cnt=0;

		n++;

		if(n==1)n++;

		vector<int>primes;

		vector<int>sums;

		while(true){

			if(is(n)){

				cnt++;

				primes.push_back(n);

			}

			if(cnt==78)break;

			n++;

		}

		//for(int i=0;i<primes.size();i++)cout<<primes[i]<<endl;

		for(int i=0;i<primes.size();i++){

			for(int j=0;j<primes.size();j++){

				if(j>primes.size())break;

				if(i<j)break;

				sums.push_back(primes[i]+primes[j]);

			}

		}

		sort(sums.begin(),sums.end());

		cout<<sums[p-1]<<endl;

	}

	return 0;

}

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

#include<vector>

#include<algorithm>

#include<functional>

using namespace std;

	bool is(int x){

		for(int i=2;i*i<=x;i++)if(x%i==0)return false;

		return true;

	}

int main(){

	int n,p;



	while(cin>>n>>p,n!=-1||p!=-1){

		int cnt=0;

		n++;

		if(n==1)n++;

		vector<int>primes;

		vector<int>sums;

		while(true){

			if(is(n)){

				cnt++;

				primes.push_back(n);

			}

			if(cnt==22)break;

			n++;

		}

		//for(int i=0;i<primes.size();i++)cout<<primes[i]<<endl;

		for(int i=0;i<primes.size();i++){

			for(int j=0;j<primes.size();j++){

				if(j>primes.size())break;

				if(i<j)break;

				sums.push_back(primes[i]+primes[j]);

			}

		}

		sort(sums.begin(),sums.end());

		cout<<sums[p-1]<<endl;

	}

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;



const int N = 100005;

ll n, C, x[N], v[N], pre[N], ans, p, q, ans1, ans2;

ll a[N], b[N], L[N], R[N];



int main() {

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

    // 1

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

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

        pre[i] = pre[i - 1] + v[i];

    }

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

        ll tmp = pre[i] - x[i];

        a[i] = tmp;

        ans1 = max(ans1, tmp);

    }

    ans = max(ans, ans1);



    // 2

    for (int i = 1; i <= n; i++) pre[i] = v[i];

    for (int i = n; i >= 1; i--) pre[i] += pre[i + 1];

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

        ll tmp = pre[i] - (C - x[i]);

        b[i] = tmp;

        ans2 = max(ans2, tmp);

    }

    ans = max(ans, ans2);



    // 3

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

        L[i] = max(L[i - 1], a[i] - x[i]);

    }

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

        R[i] = max(R[i + 1], b[i]);

    }

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

        ans = max(ans, L[i] + R[i + 1]);



    // 4

    memset(L, 0, sizeof(L));

    memset(R, 0, sizeof(R));

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

        L[i] = max(L[i - 1], a[i]);

    }

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

        R[i] = max(R[i + 1], b[i] - (C - x[i]));

    }

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

        ans = max(ans, L[i] + R[i + 1]);





    printf("%lld\n", ans);

    return 0;

}


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

using namespace std;



template<typename T>

void read(T &ans){T f = 1; char c = getchar();ans = 0;while(!isdigit(c)){if(c == '-')f = -1;c = getchar();}while( isdigit(c)){ans = ans*10 + c - '0';c = getchar();}ans *= f;}

#define int long long 

const int maxn = 1e5+5;

int n, x[maxn], v[maxn], c;

int dir1[maxn], dir2[maxn];

int mx_dir1[maxn], mx_dir2[maxn];



signed main(){

    read(n), read(c);

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

        read(x[i]), read(v[i]);

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

        mx_dir1[i] = max(mx_dir1[i-1], dir1[i]);

    }

    x[n+1] = c;

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

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

        mx_dir2[i] = max(mx_dir2[i+1], dir2[i]);

	}

	

	int ans = 0;

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

		ans = max(ans, mx_dir1[i] + dir2[i+1] - (c - x[i+1]));

		ans = max(ans, mx_dir2[i] + dir1[i-1] - x[i-1]); 

	}

    cout<<ans<<endl;

    return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;



const int N = 100005;

ll n, C, x[N], v[N], pre[N], ans, p, q, ans1, ans2;

ll a[N], b[N], L[N], R[N];



int main() {

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

    // 1

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

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

        pre[i] = pre[i - 1] + v[i];

    }

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

        ll tmp = pre[i] - x[i];

        a[i] = tmp;

        ans1 = max(ans1, tmp);

    }

    ans = max(ans, ans1);



    // 2

    for (int i = 1; i <= n; i++) pre[i] = v[i];

    for (int i = n; i >= 1; i--) pre[i] += pre[i + 1];

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

        ll tmp = pre[i] - (C - x[i]);

        b[i] = tmp;

        ans2 = max(ans2, tmp);

    }

    ans = max(ans, ans2);



    // 3

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

        L[i] = max(L[i - 1], a[i] - x[i]);

    }

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

        R[i] = max(R[i + 1], b[i]);

    }

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

        ans = max(ans, L[i] + R[i + 1]);



    // 4

    memset(L, 0, sizeof(L));

    memset(R, 0, sizeof(R));

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

        L[i] = max(L[i - 1], a[i]);

    }

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

        R[i] = max(R[i + 1], b[i] - (C - x[i]));

    }

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

        ans = max(ans, L[i] + R[i + 1]);





    printf("%lld\n", ans);

    return 0;

}


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

#define ll long long

using namespace std;

//int v[100010],x[100010];

ll n,c;

ll l[100010],r[100010];

struct node{

	ll v,x;

}a[100010];

//bool cmp(node x,node y){return x.x<y.x;}

int main(){

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

	for(int i=1;i<=n;i++) scanf("%lld%lld",&a[i].x,&a[i].v);

//	sort(a+1,a+n+1,cmp);

	for(int i=1;i<=n;i++)l[i]=l[i-1]-a[i].x+a[i-1].x+a[i].v;

//	for(int i=1;i<=n;i++)printf("%d ",l[i]);

//	puts("");

	for(int i=2;i<=n;i++)l[i]=max(l[i],l[i-1]);

	a[n+1].x=c;

	ll ans=0;

	for(int i=n;i>=1;i--) r[i]=r[i+1]+a[i].v+a[i].x-a[i+1].x;

//	for(int i=1;i<=n;i++) printf("%d ",r[i]);

//	puts("");

	for(int i=n-1;i>=1;i--) r[i]=max(r[i],r[i+1]);

	for(int i=1;i<=n;i++) ans=max(ans,l[i]);

	for(int i=1;i<=n;i++) ans=max(ans,r[i]);

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

		ll k=c-a[i].x;

		ans=max(ans,r[i]+l[i-1]-k);

	}

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

		ll k=a[i].x;

		ans=max(ans,r[i+1]+l[i]-k);

	}

	printf("%lld",ans);

	return 0;

}
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;



const int N = 100005;

ll n, C, x[N], v[N], pre[N], ans, p, q, ans1, ans2;

ll a[N], b[N], L[N], R[N];



int main() {

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

    // 1

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

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

        pre[i] = pre[i - 1] + v[i];

    }

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

        ll tmp = pre[i] - x[i];

        a[i] = tmp;

        ans1 = max(ans1, tmp);

    }

    ans = max(ans, ans1);



    // 2

    for (int i = 1; i <= n; i++) pre[i] = v[i];

    for (int i = n; i >= 1; i--) pre[i] += pre[i + 1];

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

        ll tmp = pre[i] - (C - x[i]);

        b[i] = tmp;

        ans2 = max(ans2, tmp);

    }

    ans = max(ans, ans2);



    // 3

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

        L[i] = max(L[i - 1], a[i] - x[i]);

    }

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

        R[i] = max(R[i + 1], b[i]);

    }

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

        ans = max(ans, L[i] + R[i + 1]);



    // 4

    memset(L, 0, sizeof(L));

    memset(R, 0, sizeof(R));

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

        L[i] = max(L[i - 1], a[i]);

    }

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

        R[i] = max(R[i + 1], b[i] - (C - x[i]));

    }

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

        ans = max(ans, L[i] + R[i + 1]);





    printf("%lld\n", ans);

    return 0;

}


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

namespace chtholly{

typedef long long ll;

#define re0 register int

#define rec register char

#define rel register ll

#define gc getchar

#define pc putchar

#define p32 pc(' ')

#define pl puts("")

/*By Citrus*/

inline int read(){

  int x=0,f=1;char c=gc();

  for (;!isdigit(c);c=gc()) f^=c=='-';

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return f?x:-x;

  }

template <typename mitsuha>

inline bool read(mitsuha &x){

  x=0;int f=1;char c=gc();

  for (;!isdigit(c)&&~c;c=gc()) f^=c=='-';

  if (!~c) return 0;

  for (;isdigit(c);c=gc()) x=(x<<3)+(x<<1)+(c^'0');

  return x=f?x:-x,1;

  }

template <typename mitsuha>

inline int write(mitsuha x){

  if (!x) return pc(48);

  if (x<0) x=-x,pc('-');

  int bit[20],i,p=0;

  for (;x;x/=10) bit[++p]=x%10;

  for (i=p;i;--i) pc(bit[i]+48);

  return 0;

  }

inline char fuhao(){

  char c=gc();

  for (;isspace(c);c=gc());

  return c;

  }

}using namespace chtholly;

using namespace std;

const int yuzu=1e5;

typedef ll fuko[yuzu|10];

int n=read();ll c;

fuko x,v,a,b,d,e;

int main(){

int i;

read(c);

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

  read(x[i]),read(v[i]);

  } 

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

  a[i]=a[i-1]+x[i-1]-x[i]+v[i];

  }

x[n+1]=c;

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

  b[i]=b[i+1]-x[i+1]+x[i]+v[i];

  }

for (i=2;i<=n;++i) a[i]=max(a[i],a[i-1]);

for (i=n-1;i;--i) b[i]=max(b[i],b[i+1]);

ll ans=0;

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

  ans=max(ans,max(a[i],max(b[i],max(a[i]-x[i]+b[i+1],b[i]-c+x[i]+a[i-1]))));

  }

write(ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;



const int N = 100005;

ll n, C, x[N], v[N], pre[N], ans, p, q, ans1, ans2;

ll a[N], b[N], L[N], R[N];



int main() {

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

    // 1

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

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

        pre[i] = pre[i - 1] + v[i];

    }

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

        ll tmp = pre[i] - x[i];

        a[i] = tmp;

        ans1 = max(ans1, tmp);

    }

    ans = max(ans, ans1);



    // 2

    for (int i = 1; i <= n; i++) pre[i] = v[i];

    for (int i = n; i >= 1; i--) pre[i] += pre[i + 1];

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

        ll tmp = pre[i] - (C - x[i]);

        b[i] = tmp;

        ans2 = max(ans2, tmp);

    }

    ans = max(ans, ans2);



    // 3

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

        L[i] = max(L[i - 1], a[i] - x[i]);

    }

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

        R[i] = max(R[i + 1], b[i]);

    }

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

        ans = max(ans, L[i] + R[i + 1]);



    // 4

    memset(L, 0, sizeof(L));

    memset(R, 0, sizeof(R));

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

        L[i] = max(L[i - 1], a[i]);

    }

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

        R[i] = max(R[i + 1], b[i] - (C - x[i]));

    }

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

        ans = max(ans, L[i] + R[i + 1]);





    printf("%lld\n", ans);

    return 0;

}


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



using namespace std ;



int main()

{

	ios_base::sync_with_stdio(0) ;

	cin.tie(0) ;

	int n ;

	long long m ;

	cin>>n>>m ;

	long long a[n] , b[n] ;

	for(int i = 0 ; i < n ; ++i)

		cin>>a[i]>>b[i] ;

	long long pref[n] , suff[n] ;

	long long sum = 0 ;

	long long prv = 0 ;

	for(int i = 0 ; i < n ; ++i)

	{

		sum += b[i] ;

		sum -= (a[i] - prv) ;

		if(i == 0)

			pref[i] = max(0ll , sum) ;

		else

			pref[i] = max(pref[i-1] , sum) ;

		prv = a[i] ;

	}

	sum = 0 ;

	prv = m ;

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

	{

		sum += b[i] ;

		sum -= (prv - a[i]) ;

		if(i == n-1)

			suff[i] = max(0ll , sum) ;

		else

			suff[i] = max(suff[i+1] , sum) ;

		prv = a[i] ;

	}

	sum = 0ll , prv = 0ll ;

	long long ans = 0ll ;

	for(int i = 0 ; i < n ; ++i)

	{

		sum += b[i] ;

		sum -= (a[i] - prv) ;

		long long x = 0 ;

		if(i != n-1)

			x = suff[i+1] - a[i] ;

		ans = max(ans , max(sum , sum+x)) ;

		prv = a[i] ;

	}

	sum = 0ll , prv = m ;

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

	{

		sum += b[i] ;

		sum -= (prv - a[i]) ;

		long long x = 0 ;

		if(i != 0)

			x = pref[i-1] - (m - a[i]) ;

		ans = max(ans , max(sum , sum+x)) ;

		prv = a[i] ;

	}

	return cout<<ans<<"\n" , 0 ;

}
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;



const int N = 100005;

ll n, C, x[N], v[N], pre[N], ans, p, q, ans1, ans2;

ll a[N], b[N], L[N], R[N];



int main() {

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

    // 1

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

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

        pre[i] = pre[i - 1] + v[i];

    }

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

        ll tmp = pre[i] - x[i];

        a[i] = tmp;

        ans1 = max(ans1, tmp);

    }

    ans = max(ans, ans1);



    // 2

    for (int i = 1; i <= n; i++) pre[i] = v[i];

    for (int i = n; i >= 1; i--) pre[i] += pre[i + 1];

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

        ll tmp = pre[i] - (C - x[i]);

        b[i] = tmp;

        ans2 = max(ans2, tmp);

    }

    ans = max(ans, ans2);



    // 3

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

        L[i] = max(L[i - 1], a[i] - x[i]);

    }

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

        R[i] = max(R[i + 1], b[i]);

    }

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

        ans = max(ans, L[i] + R[i + 1]);



    // 4

    memset(L, 0, sizeof(L));

    memset(R, 0, sizeof(R));

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

        L[i] = max(L[i - 1], a[i]);

    }

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

        R[i] = max(R[i + 1], b[i] - (C - x[i]));

    }

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

        ans = max(ans, L[i] + R[i + 1]);





    printf("%lld\n", ans);

    return 0;

}


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



using namespace std ;



const int MAX = 1e5 + 10 ;



long long arr[MAX] , val[MAX] , pref[MAX] , suff[MAX] ;

long long bestpref[MAX] , bestsuff[MAX] ;

long long n , c ;



int main()

{

	ios_base::sync_with_stdio(0) ;

	cin.tie(0) ;

	cin>>n>>c ;

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

		cin>>arr[i]>>val[i] ;

	pref[0] = 0 ;

	arr[0] = 0 ;

	bestpref[0] = 0 ;

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

	{

		pref[i] = pref[i-1] - (arr[i] - arr[i-1]) + val[i] ;

		bestpref[i] = max(bestpref[i-1] , pref[i]) ;

	}

	suff[n+1] = 0 ;

	arr[n+1] = c ;

	bestsuff[n+1] = 0 ;

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

	{

		suff[i] = suff[i+1] - (arr[i+1] - arr[i]) + val[i] ;

		bestsuff[i] = max(bestsuff[i+1] , suff[i]) ;

	}

	long long ans = 0ll ;

	for(int i = 0 ; i <= n ; ++i)

	{

		ans = max(ans , pref[i]) ;

		ans = max(ans , pref[i] - arr[i] + bestsuff[i+1]) ;

	}

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

	{

		ans = max(ans , suff[i]) ;

		ans = max(ans , suff[i] - (c - arr[i]) + bestpref[i-1]) ;

	}

	return cout<<ans<<"\n" , 0 ;

}
// Slower C++ solution:
#include <bits/stdc++.h>

typedef long long ll;

using namespace std;



const int N = 100005;

ll n, C, x[N], v[N], pre[N], ans, p, q, ans1, ans2;

ll a[N], b[N], L[N], R[N];



int main() {

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

    // 1

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

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

        pre[i] = pre[i - 1] + v[i];

    }

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

        ll tmp = pre[i] - x[i];

        a[i] = tmp;

        ans1 = max(ans1, tmp);

    }

    ans = max(ans, ans1);



    // 2

    for (int i = 1; i <= n; i++) pre[i] = v[i];

    for (int i = n; i >= 1; i--) pre[i] += pre[i + 1];

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

        ll tmp = pre[i] - (C - x[i]);

        b[i] = tmp;

        ans2 = max(ans2, tmp);

    }

    ans = max(ans, ans2);



    // 3

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

        L[i] = max(L[i - 1], a[i] - x[i]);

    }

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

        R[i] = max(R[i + 1], b[i]);

    }

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

        ans = max(ans, L[i] + R[i + 1]);



    // 4

    memset(L, 0, sizeof(L));

    memset(R, 0, sizeof(R));

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

        L[i] = max(L[i - 1], a[i]);

    }

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

        R[i] = max(R[i + 1], b[i] - (C - x[i]));

    }

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

        ans = max(ans, L[i] + R[i + 1]);





    printf("%lld\n", ans);

    return 0;

}


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



using namespace std;

const int N=5e5+5,mod=1e9+7;

#define ll long long

ll x[N],v[N];

ll best[N];

int main()

{

    ios_base::sync_with_stdio(0);

    cin.tie(0);cout.tie(0);

    int n;

    ll c;

    cin>>n>>c;

    for(int i=0;i<n;i++)

        cin>>x[i]>>v[i];

    ll cur=0,ans=0;

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

    {

        cur+=v[i];

        ans=max(ans,cur-(c-x[i]));

        best[i]=max(best[i+1],cur-(c-x[i]));

    }

    cur=0;

    for(int i=0;i<n;i++)

    {

        cur+=v[i];

        ans=max(ans,cur-x[i]);

        ans=max(ans,cur+best[i+1]-2*x[i]);

        best[i]=cur-x[i];

        if(i) best[i]=max(best[i],best[i-1]);

    }

    cur=0;

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

    {

        cur+=v[i];

        ans=max(ans,cur+best[i-1]-2*(c-x[i]));

    }

    cout<<ans<<endl;

    return 0;

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

#include <set>

#include <map>

#include <vector>

#include <algorithm>

#include <iomanip>

#include <climits>

#include <numeric>

#include <cmath>

#include <queue>

#include <sstream>



using namespace std;

typedef long long ll;



int main() {

  ll N, C;

  cin >> N >> C;

  vector<ll> x(N + 2), v(N + 2);

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

    cin >> x[i] >> v[i];

  }

  x[0] = 0;

  x[N + 1] = C;

  

  vector<vector<ll>> dp(2, vector<ll>(100010, 0));

  ll cal = 0;

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

    cal -= x[i] - x[i - 1];

    cal += v[i];

    dp[0][i] = max(dp[0][i - 1], cal);

  }

  cal = 0;

  for (ll i = N; i >= 1; i--) {

    cal -= x[i + 1] - x[i];

    cal += v[i];

    dp[1][i] = max(dp[1][i + 1], cal);

  }

  

  ll maxcal = 0;

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

    cal = dp[0][i] + dp[1][i + 1] - x[i];

    maxcal = max(maxcal, cal);

    

    cal = dp[0][i] + dp[1][i + 1] - C + x[i + 1];

    maxcal = max(maxcal, cal);

  }

  cout << maxcal << endl;

  return 0;

}


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



using namespace std;

typedef long long ll;



const ll INF = 1e9;

const ll MOD = 1e9 + 7;



int main() {

  int N;

  ll C;

  cin >> N >> C;



  vector<ll> x(N + 1, 0), v(N + 1, 0), rwv(N + 1, 0);

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

    cin >> x[i] >> v[i];

  }



  // 累積和

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

    rwv[i + 1] = rwv[i] + v[i + 1];

  }



  vector<ll> r(N + 1, 0);

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

    r[i + 1] = max(r[i], rwv[i + 1] - x[i + 1]);

  }



  vector<ll> l(N + 1, 0);

  l[N] = rwv[N] - rwv[N - 1] - (C - x[N]);

  for (int i = N - 1; i >= 1; i--) {

    l[i] = max(l[i + 1], rwv[N] - rwv[i - 1] - (C - x[i]));

  }



  ll ans = 0;

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

    ans = max(ans, rwv[i] - x[i]);

    if (i < N) {

      ans = max(ans, rwv[i] - 2 * x[i] + l[i + 1]);

    }

  }



  for (int i = N - 1; i >= 0; i--) {

    ans = max(ans, rwv[N] - rwv[i] - (C - x[i + 1]));

    if (i > 0) {

      ans = max(ans, rwv[N] - rwv[i] - 2 * (C - x[i + 1]) + r[i]);

    }

  }

  cout << ans << endl;



  return 0;

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

#include <set>

#include <map>

#include <vector>

#include <algorithm>

#include <iomanip>

#include <climits>

#include <numeric>

#include <cmath>

#include <queue>

#include <sstream>



using namespace std;

typedef long long ll;



int main() {

  ll N, C;

  cin >> N >> C;

  vector<ll> x(N + 2), v(N + 2);

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

    cin >> x[i] >> v[i];

  }

  x[0] = 0;

  x[N + 1] = C;

  

  vector<vector<ll>> dp(2, vector<ll>(100010, 0));

  ll cal = 0;

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

    cal -= x[i] - x[i - 1];

    cal += v[i];

    dp[0][i] = max(dp[0][i - 1], cal);

  }

  cal = 0;

  for (ll i = N; i >= 1; i--) {

    cal -= x[i + 1] - x[i];

    cal += v[i];

    dp[1][i] = max(dp[1][i + 1], cal);

  }

  

  ll maxcal = 0;

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

    cal = dp[0][i] + dp[1][i + 1] - x[i];

    maxcal = max(maxcal, cal);

    

    cal = dp[0][i] + dp[1][i + 1] - C + x[i + 1];

    maxcal = max(maxcal, cal);

  }

  cout << maxcal << endl;

  return 0;

}


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



using namespace std;

typedef long long ll;



const ll INF = 1e9;

const ll MOD = 1e9 + 7;



int main() {

  int N;

  ll C;

  cin >> N >> C;



  vector<ll> x(N + 1, 0), v(N + 1, 0), rwv(N + 1, 0);

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

    cin >> x[i] >> v[i];

  }



  // 累積和

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

    rwv[i + 1] = rwv[i] + v[i + 1];

  }



  vector<ll> r(N + 1, 0);

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

    r[i + 1] = max(r[i], rwv[i + 1] - x[i + 1]);

  }



  vector<ll> l(N + 1, 0);

  l[N] = rwv[N] - rwv[N - 1] - (C - x[N]);

  for (int i = N - 1; i >= 1; i--) {

    l[i] = max(l[i + 1], rwv[N] - rwv[i - 1] - (C - x[i]));

  }



  ll ans = 0;

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

    ans = max(ans, rwv[i] - x[i]);

    if (i < N) {

      ans = max(ans, rwv[i] - 2 * x[i] + l[i + 1]);

    }

  }



  for (int i = N - 1; i >= 0; i--) {

    ans = max(ans, rwv[N] - rwv[i] - (C - x[i + 1]));

    if (i > 0) {

      ans = max(ans, rwv[N] - rwv[i] - 2 * (C - x[i + 1]) + r[i]);

    }

  }

  cout << ans << endl;



  return 0;

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

#include <set>

#include <map>

#include <vector>

#include <algorithm>

#include <iomanip>

#include <climits>

#include <numeric>

#include <cmath>

#include <queue>

#include <sstream>



using namespace std;

typedef long long ll;



int main() {

  ll N, C;

  cin >> N >> C;

  vector<ll> x(N + 2), v(N + 2);

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

    cin >> x[i] >> v[i];

  }

  x[0] = 0;

  x[N + 1] = C;

  

  vector<vector<ll>> dp(2, vector<ll>(100010, 0));

  ll cal = 0;

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

    cal -= x[i] - x[i - 1];

    cal += v[i];

    dp[0][i] = max(dp[0][i - 1], cal);

  }

  cal = 0;

  for (ll i = N; i >= 1; i--) {

    cal -= x[i + 1] - x[i];

    cal += v[i];

    dp[1][i] = max(dp[1][i + 1], cal);

  }

  

  ll maxcal = 0;

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

    cal = dp[0][i] + dp[1][i + 1] - x[i];

    maxcal = max(maxcal, cal);

    

    cal = dp[0][i] + dp[1][i + 1] - C + x[i + 1];

    maxcal = max(maxcal, cal);

  }

  cout << maxcal << endl;

  return 0;

}


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



using namespace std;

typedef long long ll;



const ll INF = 1e9;

const ll MOD = 1e9 + 7;



int main() {

  int N;

  ll C;

  cin >> N >> C;



  vector<ll> x(N + 1, 0), v(N + 1, 0), rwv(N + 1, 0);

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

    cin >> x[i] >> v[i];

  }



  // 累積和

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

    rwv[i + 1] = rwv[i] + v[i + 1];

  }



  vector<ll> r(N + 1, 0);

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

    r[i + 1] = max(r[i], rwv[i + 1] - x[i + 1]);

  }



  vector<ll> l(N + 1, 0);

  l[1] = rwv[N] - rwv[N - 1] - (C - x[N]);

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

    l[i + 1] = max(l[i], rwv[N] - rwv[N - (i + 1)] - (C - x[N - i]));

  }



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

  //   cout << l[i] << " ";

  // }

  // cout << endl;

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

  //   cout << r[i] << " ";

  // }

  // cout << endl;





  ll ans = 0;

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

    ans = max(ans, rwv[i] - x[i]);

    if (i < N) {

      ans = max(ans, rwv[i] - 2 * x[i] + l[N - i]);

    }

  }



  for (int i = N - 1; i >= 0; i--) {

    ans = max(ans, rwv[N] - rwv[i] - (C - x[i + 1]));

    if (i > 0) {

      ans = max(ans, rwv[N] - rwv[i] - 2 * (C - x[i + 1]) + r[i]);

    }

  }

  cout << ans << endl;



  return 0;

}