题目描述
贪心
import java.util.*;
public class Main {
static class Pair {
int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
Pair[] a = new Pair[n];
for (int i = 0; i < n; i++) {
int w = scanner.nextInt();
int s = scanner.nextInt();
a[i] = new Pair(w + s, w);
}
// 使用lambda表达式进行比较并排序
Arrays.sort(a, (x, y) -> x.x - y.x);
int res = Integer.MIN_VALUE;
int sum = 0;
for (int i = 0; i < n; i++) {
int w = a[i].y;
int s = a[i].x - w;
res = Math.max(res, sum - s);
sum += w;
}
System.out.println(res);
}
}