B. No Time for Dragons----排序水题

B. No Time for Dragons
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One fairy king hated dragons to death. Not only that these monsters burn whole villages to ashes, kidnap princesses and guard treasures that they don't need at all, but they are also mentioned in statements of programming problems very often. To end their tyranny, he decided to recruit an army and destroy these damned creatures once and forever.

The king found out that there are n dragons in total, and to defeat the i-th of them he needs an army of ai soldiers, bi of which will be killed during the battle. Now he wants to know the minimal number of soldiers he needs to recruit in order to kill all the dragons. The king doesn't care about the order of battles: the only thing that matters is that none of the dragons will be left alive.

Input

The first line contains a single integer n (1 ≤ n ≤ 2·105) — the number of dragons.

Each of the next n lines contains two space-separated integers: ai and bi (1 ≤ bi ≤ ai ≤ 109) — the number of soldiers needed to defeat the i-th dragon, and the number of soldiers that will be killed in the battle against him.

Output

Output a single integer — the minimal number of soldiers that is sufficient to kill all the dragons.

Examples
input
2
7 4
5 1
output
8
input
3
4 1
6 4
5 3
output
10

题目链接:http://codeforces.com/gym/101149/problem/B


屠龙题,杀掉一只龙需要n个人,杀死后死m个人,求最少几人。

找了半天坑点没有发现这个题坑在哪,不知道上午比赛为什么他们每个人至少wa了一发。


排序水题。。。。

代码:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
using namespace std;
struct node{
    LL a,b,c;
}xin[300000];
bool cmp(struct node a,struct node b){
    if(a.c==b.c)
        return a.a<b.a;
    return a.c>b.c;
}
int main(){
    int n;
    while(~scanf("%d",&n)){
        for(int i=0;i<n;i++){
            scanf("%lld%lld",&xin[i].a,&xin[i].b);
            xin[i].c=xin[i].a-xin[i].b;
        }
        sort(xin,xin+n,cmp);
        LL ans=0;
        LL xian=0;
        for(int i=0;i<n;i++){
            if(xian<xin[i].a){
                ans+=(xin[i].a-xian);
                xian=xin[i].a;
            }
            xian-=xin[i].b;
        }
        cout<<ans<<endl;
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值