#include <bits/stdc++.h> using namespace std; #define PB push_back #define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i) #define SZ(x) int((x).size()) #define REP(i, n) for(int i=0,_=(n);i<_;++i) #define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i) typedef long long ll; typedef vector<ll> vll; typedef pair<ll, ll> pll; const ll INF = 9e18 + 9; void inline one() { int n; cin >> n; ll a[n], b[n]; pll t[n]; REP (i, n) { // cin >> a[i] >> b[i]; cin >> t[i].first >> t[i].second; } sort(t, t + n); REP (i, n) { a[i] = t[i].first; b[i] = t[i].second; // cout << a[i] << " " << b[i] << "\n"; } ll result[n]; REP (i, n) { result[i] = 0; } REP (i, n) { result[n - 1] += a[i] * (ll)i + b[i]; } vll left; REP (i, n) { left.PB(i); } FORD (i, n - 2, 0) { ll lowest_loss = INF; ll worst_position_in_left = -1; ll sum_a = 0; FORD (j, SZ(left) - 1, 0) { int x = left[j]; ll loss = a[x] * j + b[x] + sum_a; if (loss < lowest_loss) { lowest_loss = loss; worst_position_in_left = j; } sum_a += a[x]; } result[i] = result[i + 1] - lowest_loss; left.erase(left.begin() + worst_position_in_left); } REP (i, n) { cout << result[i] << "\n"; } } int main() { ios::sync_with_stdio(false); //int z; cin >> z; while(z--) one(); }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | #include <bits/stdc++.h> using namespace std; #define PB push_back #define FORE(i, t) for(__typeof(t.begin())i=t.begin();i!=t.end();++i) #define SZ(x) int((x).size()) #define REP(i, n) for(int i=0,_=(n);i<_;++i) #define FOR(i, a, b) for(int i=(a),_=(b);i<=_;++i) #define FORD(i, a, b) for(int i=(a),_=(b);i>=_;--i) typedef long long ll; typedef vector<ll> vll; typedef pair<ll, ll> pll; const ll INF = 9e18 + 9; void inline one() { int n; cin >> n; ll a[n], b[n]; pll t[n]; REP (i, n) { // cin >> a[i] >> b[i]; cin >> t[i].first >> t[i].second; } sort(t, t + n); REP (i, n) { a[i] = t[i].first; b[i] = t[i].second; // cout << a[i] << " " << b[i] << "\n"; } ll result[n]; REP (i, n) { result[i] = 0; } REP (i, n) { result[n - 1] += a[i] * (ll)i + b[i]; } vll left; REP (i, n) { left.PB(i); } FORD (i, n - 2, 0) { ll lowest_loss = INF; ll worst_position_in_left = -1; ll sum_a = 0; FORD (j, SZ(left) - 1, 0) { int x = left[j]; ll loss = a[x] * j + b[x] + sum_a; if (loss < lowest_loss) { lowest_loss = loss; worst_position_in_left = j; } sum_a += a[x]; } result[i] = result[i + 1] - lowest_loss; left.erase(left.begin() + worst_position_in_left); } REP (i, n) { cout << result[i] << "\n"; } } int main() { ios::sync_with_stdio(false); //int z; cin >> z; while(z--) one(); } |