#include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define MP make_pair #define PB push_back #define pii pair<int,int> #define vi vector<int> #define slong long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define ALL(X) (X).begin(), (X).end() #define fi first #define se second const int N = 1024*1024; const slong INF = 1000000000LL * 1000000000LL; multiset<pair<slong, slong>> D,S; slong extra; slong best_value() { int i = 0; auto it = S.begin(); pair<slong, pair<slong, slong>> best = {-1,{0,0}}; slong add = extra; for(auto d : D) { while(it->first < d.first) { i++; add -= it->first; it++; } best = max(best, MP(d.first*i+add+d.second,d)); } D.erase(D.find(best.second)); S.insert(best.second); extra += best.second.first; return best.first; } int main () { int n; scanf("%d",&n); FOR(i,1,n) { slong a,b; scanf("%lld%lld",&a,&b); D.insert(MP(a,b)); } slong sum = 0; S.insert({INF,INF}); FOR(i, 1, n) { sum += best_value(); printf("%lld\n", sum); } }
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 | #include <bits/stdc++.h> using namespace std; #define FOR(i,a,b) for(int i=(a);i<=(b);++i) #define FORD(i,a,b) for(int i=(a);i>=(b);--i) #define MP make_pair #define PB push_back #define pii pair<int,int> #define vi vector<int> #define slong long long #define SZ(x) int((x).size()) #define DBG(v) cerr << #v << " = " << (v) << endl; #define ALL(X) (X).begin(), (X).end() #define fi first #define se second const int N = 1024*1024; const slong INF = 1000000000LL * 1000000000LL; multiset<pair<slong, slong>> D,S; slong extra; slong best_value() { int i = 0; auto it = S.begin(); pair<slong, pair<slong, slong>> best = {-1,{0,0}}; slong add = extra; for(auto d : D) { while(it->first < d.first) { i++; add -= it->first; it++; } best = max(best, MP(d.first*i+add+d.second,d)); } D.erase(D.find(best.second)); S.insert(best.second); extra += best.second.first; return best.first; } int main () { int n; scanf("%d",&n); FOR(i,1,n) { slong a,b; scanf("%lld%lld",&a,&b); D.insert(MP(a,b)); } slong sum = 0; S.insert({INF,INF}); FOR(i, 1, n) { sum += best_value(); printf("%lld\n", sum); } } |