Language: C++

#include <bits/stdc++.h>
using namespace std;

typedef long long lint;
typedef unsigned long long ulint;
const int MOD=1000000007;

bool pc(const pair<lint,lint>& a,const pair<lint,lint>& b){
    return a.second<b.second;
}
bool gpc(const pair<lint,lint>& a,const pair<lint,lint>& b){
    return a.second>b.second;
}

int main(){
    ulint N,C;
    ulint x[100002],v[100002];
    cin >> N >> C;
    x[0]=v[0]=0LL;
    x[N+1]=C;
    v[N+1]=0LL;
    for(lint i=1;i<=N;i++) cin >> x[i] >> v[i];

    pair<lint,lint> right[100002],left[100002],rb[100002],lb[100002];
    right[0]=rb[0]=make_pair(0,0);
    left[N+1]=lb[N+1]=make_pair(0,0);

    for(lint i=1;i<=N+1;i++){
        right[i]=make_pair(right[i-1].first+v[i]-(x[i]-x[i-1]),i);
        rb[i]=make_pair(right[i-1].first+v[i]-(x[i]-x[i-1])-x[i],i);
    }
    for(lint i=N;i>=0;i--){
        left[i]=make_pair(left[i+1].first+v[i]-(x[i+1]-x[i]),i);
        lb[i]=make_pair(left[i+1].first+v[i]-(x[i+1]-x[i])-(C-x[i]),i);
    }
    sort(right,right+N+2,greater<pair<lint,lint>>());
    sort(left,left+N+2,greater<pair<lint,lint>>());

    lint best=max(right[0].first,left[0].first);
    for(int i=1;i<=N;i++){
        if(rb[i].first>0){
            //best=max(best,rb[i].first+lower_bound(left,left+N+2,rb[i].second+1,pc)->first);
        }
        if(lb[i].first>0){
            best=max(best,lb[i].first+lower_bound(right,right+N+2,lb[i].second+1,gpc)->first);
            for(int j=0;j<=N+1;j++){
                if(right[j].second<lb[i].second){
                    best=max(best,lb[i].first+right[j].first);
                    break;
                }
            }
        }
    }
    cout << best << endl;
/*
    if(left[N+1].second<=right[N+1].second){
        cout << max(left[N+1].first,right[N+1].first);
    }else{
        lint X=right[N+1].first+left[N+1].first-x[right[N+1].second];
        lint Y=right[N+1].first+left[N+1].first-(C-x[left[N+1].second]);
        cout << max(max(X,Y),max(left[N+1].first,right[N+1].first));
    }
*/
    return 0;
}

Compiler output:
In file included from /usr/include/c++/14/bits/stl_algobase.h:71,
                 from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/predefined_ops.h: In instantiation of 'bool __gnu_cxx::__ops::_Iter_comp_val<_Compare>::operator()(_Iterator, _Value&) [with _Iterator = std::pair<long long int, long long int>*; _Value = const long long int; _Compare = bool (*)(const std::pair<long long int, long long int>&, const std::pair<long long int, long long int>&)]':
/usr/include/c++/14/bits/stl_algobase.h:1504:14:   required from '_ForwardIterator std::__lower_bound(_ForwardIterator, _ForwardIterator, const _Tp&, _Compare) [with _ForwardIterator = pair<long long int, long long int>*; _Tp = long long int; _Compare = __gnu_cxx::__ops::_Iter_comp_val<bool (*)(const pair<long long int, long long int>&, const pair<long long int, long long int>&)>]'
 1504 |           if (__comp(__middle, __val))
      |               ~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algo.h:1973:32:   required from '_FIter std::lower_bound(_FIter, _FIter, const _Tp&, _Compare) [with _FIter = pair<long long int, long long int>*; _Tp = long long int; _Compare = bool (*)(const pair<long long int, long long int>&, const pair<long long int, long long int>&)]'
 1973 |       return std::__lower_bound(__first, __last, __val,
      |              ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
 1974 |                                 __gnu_cxx::__ops::__iter_comp_val(__comp));
      |                                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:45:50:   required from here
   45 |             best=max(best,lb[i].first+lower_bound(right,right+N+2,lb[i].second+1,gpc)->first);
      |                                       ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/predefined_ops.h:196:30: error: invalid initialization of reference of type 'const std::pair<long long int, long long int>&' from expression of type 'const long long int'
  196 |         { return bool(_M_comp(*__it, __val)); }
      |                       ~~~~~~~^~~~~~~~~~~~~~