#include #include #include using namespace std; typedef long long ll; typedef pair P; int main() { int n; cin >> n; P p[200005]; for (int i = 0; i < n; i++) { cin >> p[i].first >> p[i].second; } int l = 0; sort(p, p + n); for (int i = 0; i < n; i++) { l = max(l, p[i].first - (i + 1)); } ll ok = l - 1, ng = 2000000000; while (abs(ok - ng) > 1) { ll k = (ok + ng) / 2; int j = 0; priority_queue, greater> que; bool f = true; for (int i = 1; i <= n; i++) { while (j < n && p[j].first <= i + k) { que.push(p[j++].second); } if (!que.size() || que.top() < i + k) { f = false; break; } que.pop(); } if (f) { ok = k; } else { ng = k; } } cout << ok - l + 1 << endl; }