很简单的一个贪心了,先找出标志,发现是Ti / Di,然后排序就行了
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 1e5 + 5;
struct cow {
int t, d;
}c[maxn];
bool cmp(cow A, cow B) {
return A.t * B.d < B.t* A.d;
}
int main() {
int n;
cin >> n;
for (int i = 1;i <= n;i++)
cin >> c[i].t >> c[i].d;
sort(c + 1, c + 1 + n, cmp);
long long ans = 0, sumtime = 0;
for (int i = 1;i <= n;i++) {
ans += sumtime * c[i].d;
sumtime += 2 * c[i].t;
}
cout << ans << endl;
return 0;
}