思路
题目
代码
#include <iostream>
#include <algorithm>
using namespace std;
typedef pair<int, int> PII; //由于题目是需要按照w+s进行排序,所以需要将w+s放到pair中
const int N = 50010;
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); //pair是按照first进行排序
int res = -2e9, sum = 0; //将最小值赋值为负无穷,sum每头牛上面所有重量的总和
for (int i = 0; i < n; i ++)
{
int w = cow[i].second, s = cow[i].first - w;
res = max(res, sum - s); //求解当前牛的危险系数,sum - 强装值
sum += w; //在加上我当前牛的重量
}
cout << res;
return 0;
}