#include #include using namespace std; int n, l, r, dp[100009]; int main() { cin.tie(0); ios_base::sync_with_stdio(false); cin >> n; for (int i = 0; i <= 100000; i++) dp[i] = -i; for (int i = 0; i < n; i++) { cin >> l >> r; for (int j = r; j >= l; j--) { dp[j] = max(dp[j], dp[j - 1]); } int val = dp[r] + r; for (int j = r + 1; j <= 100000; j++) { if (dp[j] + j >= val) break; dp[j] = val - j; } } cout << dp[100000] + 100000 << endl; return 0; }