CFGym 102299I Sobytiynyy Proyekt Casino

Description

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≤N≤105
  •  
  • 0≤pi≤109
  • for all i
  •  
  • −109≤fi≤109

for all i

Output

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

Sample Input

Input

3
1 2
3 3
2 1

Output

3

Input

1
1 -2

Output

3

Input

2
0 1
3 4

Output

-1

Hint

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.

思路:直接按bi排序,然后从1遍历到n,计算从每个地方离开的值,然后取最大值

#include<stdio.h>
#include<algorithm>
using namespace std;
#define ll long long
struct node{
    ll j,c,w;
};
node x[100005];
bool cmp(node a,node b){
    return a.c<b.c;
}
int main(){
    int n;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        int a,b;
        scanf("%d %d",&a,&b);
        x[i].j=a;
        x[i].c=b;
        x[i].w=a-b;
    }
    sort(x+1,x+n+1,cmp);
    ll ans=-999999999;
    ll dw=0;
    for(int i=1;i<=n;i++){
        dw=dw+x[i].w+x[i-1].c;
        ans=max(ans,dw);
    }
    printf("%lld\n",ans);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值