406173: GYM102299 I Sobytiynyy Proyekt Casino

Memory Limit:256 MB Time Limit:0 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

I. Sobytiynyy Proyekt Casinotime limit per test0.5 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard output

Sobytiynyy Proyekt Casino is located in San Petersburg, one of the most beautiful cities in Russia, known (besides being where ITMO is located) by its delightful Hermitage Museum. The casino's owner, a former ICPC contestant who never made it to the world finals, is creating a new game (as the traditional games are not appreciated there...) and he wants revenge on his fellow competitors, by punishing players who are trying to play his game optimally.

In each round, the players get a pair of integers $$$(p, f)$$$, where $$$p > 0$$$. The player gets (p) rubles and he or she may decide to leave the game by paying (f) rubles (or receiving (-f) rubles, if (f) is negative). Depending on how they play, players may end the game while owing money...

Note also that if the player reaches the last pair, he is forced to pay (f_N).

Help the casino's owner by finding an ordering of the (N) pairs which minimize the gains from a player who plays optimally. Your code must print how much the player would earn if she played in the ordering of pairs that minimizes her gain.

Input

You should read the number (N) of pairs in the first line of input.

After this (N) lines will follow, each with two numbers separated by a space, $$$p$$$ and $$$f$$$.

Constraints

  • $$$1 \leq N \leq 10^5$$$
  • $$$0 \leq p_i \leq 10^9$$$ for all $$$i$$$
  • $$$-10^9 \leq f_i \leq 10^9$$$ for all $$$i$$$
Output

Print a single integer: how much an optimal player would gain in an ordering of pairs that minimizes her gain.

ExamplesInput
3
1 2
3 3
2 1
Output
3
Input
1
1 -2
Output
3
Input
2
0 1
3 4
Output
-1
Note

Let us analyze the possible permutations of the sequence of pairs for the first case:

Both permutations $$$(1, 2), (3, 3), (2, 1)$$$ and $$$(3, 3), (1, 2), (2, 1)$$$ award 5 to an optimal player. The permutations $$$(2, 1), (3, 3), (1, 2)$$$ and $$$(3, 3), (2, 1), (1, 2)$$$ award at most 4.

Finally, the permutations $$$(1, 2), (2, 1), (3, 3)$$$ and $$$(2, 1), (1, 2), (3, 3)$$$ award at most 3 to a player. Thus, 3 is the smallest value that an optimal player can get in this game.

Source/Category

加入题单

上一题 下一题 算法标签: