#abc352c. C - Standing On The Shoulders

C - Standing On The Shoulders

Score: 300300 points

问题陈述

NN 个巨人,编号为 11NN。当第 ii 个巨人站立在地面上时,他们的肩膀高度是 AiA_i,头部高度是 BiB_i

你可以选择一个排列 (P1,P2,,PN)(P_1, P_2, \ldots, P_N),它是 (1,2,,N)(1, 2, \ldots, N) 的一个排列,并根据以下规则堆叠这 NN 个巨人:

  • 首先,将巨人 P1P_1 放在地上。巨人 P1P_1 的肩膀将离地面 AP1A_{P_1} 的高度,他们的头部将离地面 BP1B_{P_1} 的高度。

  • 对于 i=1,2,,N1i = 1, 2, \ldots, N - 1,按顺序将巨人 Pi+1P_{i + 1} 放在巨人 PiP_i 的肩膀上。如果巨人 PiP_i 的肩膀离地面的高度是 tt,那么巨人 Pi+1P_{i + 1} 的肩膀将离地面 t+APi+1t + A_{P_{i + 1}} 的高度,他们的头部将离地面 t+BPi+1t + B_{P_{i + 1}} 的高度。

找出顶部巨人 PNP_N 的头部离地面的最大可能高度。

以上为大语言模型 kimi 翻译,仅供参考。

Problem Statement

There are NN giants, named 11 to NN. When giant ii stands on the ground, their shoulder height is AiA_i, and their head height is BiB_i.

You can choose a permutation (P1,P2,,PN)(P_1, P_2, \ldots, P_N) of (1,2,,N)(1, 2, \ldots, N) and stack the NN giants according to the following rules:

  • First, place giant P1P_1 on the ground. The giant P1P_1's shoulder will be at a height of AP1A_{P_1} from the ground, and their head will be at a height of BP1B_{P_1} from the ground.

  • For i=1,2,,N1i = 1, 2, \ldots, N - 1 in order, place giant Pi+1P_{i + 1} on the shoulders of giant PiP_i. If giant PiP_i's shoulders are at a height of tt from the ground, then giant Pi+1P_{i + 1}'s shoulders will be at a height of t+APi+1t + A_{P_{i + 1}} from the ground, and their head will be at a height of t+BPi+1t + B_{P_{i + 1}} from the ground.

Find the maximum possible height of the head of the topmost giant PNP_N from the ground.

Constraints

  • 2N2×1052 \leq N \leq 2 \times 10^5
  • 1AiBi1091 \leq A_i \leq B_i \leq 10^9
  • All input values are integers.

Input

The input is given from Standard Input in the following format:

NN

A1A_1 B1B_1

A2A_2 B2B_2

\vdots

ANA_N BNB_N

Output

Print the answer.

Sample Input 1

3
4 10
5 8
2 9

Sample Output 1

18

If (P1,P2,P3)=(2,1,3)(P_1, P_2, P_3) = (2, 1, 3), then measuring from the ground, giant 22 has a shoulder height of 55 and a head height of 88, giant 11 has a shoulder height of 99 and a head height of 1515, and giant 33 has a shoulder height of 1111 and a head height of 1818.

The head height of the topmost giant from the ground cannot be greater than 1818, so print 1818.

Sample Input 2

5
1 1
1 1
1 1
1 1
1 1

Sample Output 2

5

Sample Input 3

10
690830957 868532399
741145463 930111470
612846445 948344128
540375785 925723427
723092548 925021315
928915367 973970164
563314352 832796216
562681294 868338948
923012648 954764623
691107436 891127278

Sample Output 3

7362669937