Language: C++

/**
SXR0aXAkI0JwbXptI3FhI3Z3I293bCNqY2IjUG0jMCNicG0jVHFkcXZvLyNCcG0jQW10bjBhY2phcWFicXZvLyNNYm16dml0MSNWdyNhdGN1am16I2tpdiNhbXF9bSNQcXUjVnd6I0F0bW14MSNQcWEjaXptI2l0dCNicHF2b2EjUXYjYnBtI3BtaWRtdmEjaXZsI3d2I21pemJwMSNFcHcjcWEjYnBtem0ja2l2I3F2Ym16a21sbSNRdiNQcWEjeHptYW12a20jbXtrbXhiI0lhI3BtI3htenVxYmJtYnBHI1BtI3N2d2VtYnAjRXBpYiMraXh4bWl6bWJwI2J3I1BxYSNrem1pYmN6bWEjSWEsI0ptbnd6bSN3eiNJbmJteiN3eiNKbXBxdmwjYnBtdTEjVnd6I2FwaXR0I2JwbXwja3d1eGlhYSNJY29wYiN3biNwcWEjc3Z3ZXRtbG9tI017a214YiNpYSNQbSNlcXR0bWJwMSNQcWEjYnB6d3ZtI2x3YnAjbXtibXZsI1dkbXojYnBtI3BtaWRtdmEjSXZsI3d2I21pemJwLyNpdmwjUG0jbm1tdG1icCNWdyNuaWJxb2NtI3F2I29jaXpscXZvI0l2bCN4em1hbXpkcXZvI2JwbXUvI053eiNQbSNxYSNicG0jVXdhYiNQcW9wMSNCcG0jQWN4em11bSMrcXYjb3R3enwsMQ==
*/
#include <bits/stdc++.h>

#define F first
#define S second
#define pb push_back

const long long MOD = 1e9 + 7;
const long long MAXN = 1e6 + 1;
using namespace std;

typedef long long ll;

long long readInt() {
    bool minus1 = false;
    long long result = 0;
    char ch;
    ch = getchar();
    while (true) {
        if (ch == '-') break;
        if (ch >= '0' && ch <= '9') break;
        ch = getchar();
    }
    if (ch == '-') minus1 = true; else result = ch-'0';
    while (true) {
        ch = getchar();
        if (ch < '0' || ch > '9') break;
        result = result*10 + (ch - '0');
    }
    if (minus1)
        return -result;
    else
        return result;
}

pair<int,int> a[MAXN];

int main() {
    int n = readInt();
    for (int i = 1; i <= n; i++) {
        a[i] = {readInt(), readInt()};
    }
    sort(a + 1, a + 1 + n);
    multiset <int> s1;
    multiset <int> s2;
    for (int i = 1; i <= n; i++) {
        s2.insert(a[i].S);
    }
    ll ans = 0;
    for (int i = 1; i < n; i ++) {
        /// min(R) - max(L) + 1
        s2.erase(s2.find(a[i].S));
        s1.insert(a[i].S);
        int min_R_1 = *s1.begin();
        int min_R_2 = *s2.begin();
        ans = max(ans, max(1ll * min_R_1 - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));
      	if (min_R_1 <= min_R_2) {
            ///checking for >
            s1.erase(s1.find(a[i].S));
          	auto nxt = s1.end();
          	if(nxt != s1.begin()) {
        		nxt--;
				ans = max(ans, max(1ll * min(1ll * a[i].S, 1ll * (*nxt)) - a[i].F + 1, 0ll) + *(s1.begin()) - a[n].F + 1, 0ll));
            }
            s1.insert(a[i].S);
        }
        if (min_R_1 >= min_R_2) {
        	//correct
          	s1.erase(s1.find(a[i].S));
          	auto nxt = s1.end();
          	if(nxt != s1.begin()) {
        		nxt--;
				ans = max(ans, max(1ll * min(1ll * a[i].S, 1ll * (*nxt)) - a[i].F + 1, 0ll) + max(1ll * min_R_2 - a[n].F + 1, 0ll));
            }
            s1.insert(a[i].S);
        }
        if (a[i].F == a[n].F) assert(0);
    }
    cout << ans;
    return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:65:143: error: expected ';' before ')' token
   65 |                                 ans = max(ans, max(1ll * min(1ll * a[i].S, 1ll * (*nxt)) - a[i].F + 1, 0ll) + *(s1.begin()) - a[n].F + 1, 0ll));
      |                                                                                                                                               ^
      |                                                                                                                                               ;
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:4:
/usr/include/c++/14/bits/stl_algobase.h: In instantiation of 'constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare) [with _Tp = long long int; _Compare = long long int]':
a.cc:65:14:   required from here
   65 |                                 ans = max(ans, max(1ll * min(1ll * a[i].S, 1ll * (*nxt)) - a[i].F + 1, 0ll) + *(s1.begin()) - a[n].F + 1, 0ll));
      |                                       ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:306:17: error: '__comp' cannot be used as a function
  306 |       if (__comp(__a, __b))
      |           ~~~~~~^~~~~~~~~~