C++
$\color{#cc33ff}{— > 算法基础课题解}$
思路:
贪心
$按照\ wi + si\ 从小到大的顺序排,最大的危险系数一定是最小的$
#include<iostream>
#include<algorithm>
using namespace std;
typedef pair<int, int> pii;
const int N = 5e5 + 10;
int n;
pii cow[N];
int main() {
cin >> n;
for (int i = 0; i < n; i ++) {
int w, s; cin >> w >> s;
cow[i] = {w + s, w};
}
sort (cow, cow + n); // wi + si 从小到大排序
int res = -2e9, sum = 0;
for (int i = 0; i < n; i ++) {
int w = cow[i].second, s = cow[i].first - w;
res = max(res, sum - s);
sum += w;
}
cout << res;
return 0;
}