AcWing 125. 耍杂技的牛JAVA,简化版
原题链接
中等
作者:
ARM
,
2020-08-24 16:42:08
,
所有人可见
,
阅读 719
java 代码
import java.io.*;
import java.lang.*;
import java.util.*;
class Main{
static int n = 0, N = 50050;
static int[] w = new int[N], s = new int[N];
static int weight = 0;
static int res = 0;
public static void main(String[] args)throws Exception{
BufferedReader buf = new BufferedReader(new InputStreamReader(System.in));
n = Integer.valueOf(buf.readLine());
for(int i = 0; i < n; ++i){
String[] info = buf.readLine().split(" ");
w[i] = Integer.valueOf(info[0]);
s[i] = Integer.valueOf(info[1]);
weight += w[i];
}
int[][] f = new int[n][2];
for(int i = 0; i < n; ++i){
f[i][0] = weight - w[i] - s[i];
f[i][1] = i;
}
Arrays.sort(f, (a, b) -> a[0] - b[0]);//确定各个牛的先后位置
int use = 0;
int max = f[0][0];
for(int i = 0; i < n; ++i){
int flag = f[i][0] - use;
max = Math.max(max, f[i][0] - use);//求出最大风险
if(flag > 0)res += flag;//求出总的风险
use += w[f[i][1]];
}
System.out.println(max);//最大风险
System.out.println(res);//总的风险
}
}