Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/ochkoz-ikki-dastgoh

Two machines

Hard 1000 ms 128 MB 67%
Share

A workshop makes $n$ parts. Each part is processed first on machine $1$, then on machine $2$: the $i$-th part takes $a_{i}$ minutes on machine $1$ and $b_{i}$ minutes on machine $2$. Each machine works on only one part at a time and never interrupts a started job; both machines take the parts in the same order. A part moves to machine $2$ only after its work on machine $1$ is finished. Work starts at time $0$. Choose the order of the parts so that the last part is ready as early as possible. Print this time.

Schedule of the two machines: parts as blocks, idle time on the second machine and the finishing time

Input

The first line contains a positive integer $n$. Each of the next $n$ lines contains two positive integers: $a_{i}$ and $b_{i}$.

Output

One integer — the earliest time at which all parts are ready.

Note

$1 \le n \le 10^{5}$
$1 \le a_{i}, b_{i} \le 10^{9}$

Examples

Input #1
4
3 6
5 2
1 2
6 6
Output #1
18
Input #2
1
4 9
Output #2
13

Sign in to submit a solution

Signing up is free and takes a minute.