Sobytiynyy Proyekt Casino Gym - 102299I

传送门:

http://codeforces.com/gym/102299/problem/I

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)(p,f), where p>0p>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, pp and ff.

Constraints

  • 1N1051≤N≤105
  • 0pi1090≤pi≤109 for all ii
  • 109fi109−109≤fi≤109 for all ii

Output

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

Examples

Input
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)(1,2),(3,3),(2,1) and (3,3),(1,2),(2,1)(3,3),(1,2),(2,1) award 5 to an optimal player. The permutations (2,1),(3,3),(1,2)(2,1),(3,3),(1,2) and (3,3),(2,1),(1,2)(3,3),(2,1),(1,2) award at most 4.

Finally, the permutations (1,2),(2,1),(3,3)(1,2),(2,1),(3,3) and (2,1),(1,2),(3,3)(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.

 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 struct node
 6 {
 7     long long l ,r;
 8 }str[100005];
 9 
10 bool cmp(node a, node b)
11 {
12     return a.r<b.r;
13 }
14 
15 int main()
16 {
17     long long n;
18     scanf("%lld", &n);
19     for(long long i=0;i<n;i++)
20     {
21         scanf("%lld %lld", &str[i].l, &str[i].r);
22     }
23     sort(str, str+n, cmp);
24     long long tot = str[0].l;
25     long long maxx = tot - str[0].r;
26     
27     for(long long i=1;i<n;i++)
28     {
29         tot += str[i].l;
30         maxx = max(maxx, tot-str[i].r);
31     }
32     printf("%lld\n", maxx);
33     return 0;
34 }

 

posted @ 2019-08-21 20:41  Xxiaoyu  阅读(179)  评论(0编辑  收藏  举报