Farmer John's N (1 <= N <= 50,000) cows (numbered 1..N) are planning to run away and join the circus. Their hoofed feet prevent them from tightrope walking and swinging from the trapeze (and their last attempt at firing a cow out of a cannon met with a dismal failure). Thus, they have decided to practice performing acrobatic stunts.

The cows aren't terribly creative and have only come up with one acrobatic stunt: standing on top of each other to form a vertical stack of some height. The cows are trying to figure out the order in which they should arrange themselves ithin this stack.

Each of the N cows has an associated weight (1 <= W_i <= 10,000) and strength (1 <= S_i <= 1,000,000,000). The risk of a cow collapsing is equal to the combined weight of all cows on top of her (not including her own weight, of course) minus her strength (so that a stronger cow has a lower risk). Your task is to determine an ordering of the cows that minimizes the greatest risk of collapse for any of the cows.

Input

* Line 1: A single line with the integer N.

* Lines 2..N+1: Line i+1 describes cow i with two space-separated integers, W_i and S_i.

Output

* Line 1: A single integer, giving the largest risk of all the cows in any optimal ordering that minimizes the risk.

Sample Input

3
10 3
2 5
3 3

Sample Output

2

Hint

OUTPUT DETAILS:

Put the cow with weight 10 on the bottom. She will carry the other two cows, so the risk of her collapsing is 2+3-3=2. The other cows have lower risk of collapsing.

 
 
按牛的体重和力量之和进行排序。

因此,如果A在上,B在下,则有:
A: Ra = S + Wb - Xa;
B: Rb = S - Xb;
反之如下:
A: Ra = S - Xa;
B: Rb = S + Wa - Xb;

如果我们定义一方案好于而方案则有:
S + Wb - Xa < S + Wa - Xb;
则: Wa + Xa < Wb + Xb;

 
 
#include<iostream>
#include<algorithm>
using namespace std;
struct Cow{
int weight;
int strength;
bool operator<(const Cow& other)const{
return other.weight+other.strength<weight+strength;
}
}cows[];
int main(){
int n;
cin>>n;
int total=;
for(int i=;i<n;i++){
cin>>cows[i].weight>>cows[i].strength;
total+=cows[i].weight;
}
sort(cows,cows+n);
int m=-INT_MAX;
for(int i=;i<n;i++){
total-=cows[i].weight;
m=max(m,total-cows[i].strength);
}
cout<<m<<endl;
return ;
}

最新文章

  1. Hive安装(二)之表不见了
  2. 网络初见&amp;网络监测
  3. 1.使用using释放资源
  4. Mac 下用IDEA时maven,ant打包 (mr 入库hbase)
  5. ios内购
  6. Android开发--FrameLayout的应用
  7. 剑指Offer26 字符串的全排列
  8. [转]Oracle 阳历转农历
  9. SHell命令总结
  10. qt 自动完成LineEdit
  11. jsJqGrid
  12. SimpleDateFormat将月/日/年 时分秒转换为年-月-日 时:分:秒
  13. SVN使用教程总结(转载)
  14. C#在SharePoint文档库下动态新增文件夹
  15. [EXP]Cisco RV110W - Password Disclosure / Command Execution
  16. UVa 11846 - Finding Seats Again
  17. Fluent动网格【6】:部件变形案例
  18. 20165324_mypwd
  19. php 数据类型转换与比较
  20. springcloud 出现unavailable-replicas

热门文章

  1. dev NavBarControl控件
  2. devexpress WinForms MVVM
  3. maven的父工程中添加子工程
  4. 数组转xml格式/xml格式转数组
  5. Spring PropertyResolver 占位符解析(一)API 介绍
  6. Best Sightseeing Pair LT1014
  7. xcode如何运行下载的demo工程
  8. 提升HTML5的性能体验系列之五 webview启动速度优化及事件顺序解析
  9. 使用crf++
  10. SQL语句备份和还原数据库