~/algor $ cat problems/ochkoz-ikki-dastgoh
Two machines
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.
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
4 3 6 5 2 1 2 6 6
18
1 4 9
13