// Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #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 deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; int n, S; long long the_best; struct polana { long long pocz; long long h; }; polana tab[1000007]; struct drzewo { int ile; int maks; long long maks_pocz; long long maks_h; long long suma; long long maks_zysk; }; drzewo d[2100007]; bool comp(polana a, polana b) { return a.h > b.h; } pair<int, long long> znajdz_najlepszy(int nr, long long suma_l, int ile_p) { if(d[nr].maks == 0) return MP(0, 0); if(nr >= S) { // cout << "element (" << tab[nr-S].pocz << ", " << tab[nr-S].h << ") ma ile: " << ile_p << ", sume: " << suma_l << " i daje wynik " << suma_l + tab[nr-S].pocz + ile_p*tab[nr-S].h << endl; return MP(nr-S, suma_l + tab[nr-S].pocz + ile_p*tab[nr-S].h); } else { pair <int, long long> r = MP(0, 0); long long limit_l = d[nr*2].maks_zysk + suma_l + d[2*nr].maks_h*(d[2*nr+1].ile + ile_p); long long limit_p = d[nr*2+1].maks_zysk + suma_l + d[nr].suma + d[2*nr+1].maks_h*ile_p; if(limit_l > limit_p) { //if(d[nr*2].maks_pocz + (suma_l + d[nr].suma) + d[2*nr].maks_h*(d[nr].ile + ile_p) > the_best) if(limit_l > the_best) { pair <int, long long> l = znajdz_najlepszy(nr*2, suma_l, ile_p + d[nr*2+1].ile); the_best = max(the_best, l.second); r = (r.second > l.second ? r : l); } //if(d[nr*2+1].maks_pocz + (suma_l + d[nr].suma) + d[2*nr+1].maks_h*(d[nr].ile + ile_p) > the_best) if(limit_p > the_best) { pair <int, long long> p = znajdz_najlepszy(nr*2+1, suma_l + d[nr*2].suma, ile_p); the_best = max(the_best, p.second); r = (r.second > p.second ? r : p); } } else { //if(d[nr*2+1].maks_pocz + (suma_l + d[nr].suma) + d[2*nr+1].maks_h*(d[nr].ile + ile_p) > the_best) if(limit_p > the_best) { pair <int, long long> p = znajdz_najlepszy(nr*2+1, suma_l + d[nr*2].suma, ile_p); the_best = max(the_best, p.second); r = (r.second > p.second ? r : p); } //if(d[nr*2].maks_pocz + (suma_l + d[nr].suma) + d[2*nr].maks_h*(d[nr].ile + ile_p) > the_best) if(limit_l > the_best) { pair <int, long long> l = znajdz_najlepszy(nr*2, suma_l, ile_p + d[nr*2+1].ile); the_best = max(the_best, l.second); r = (r.second > l.second ? r : l); } } return r; } } void wkladaj(int nr) { nr += S; d[nr].ile += 1; d[nr].suma += tab[nr-S].h; d[nr].maks = 0; d[nr].maks_pocz = 0; d[nr].maks_h = 0; d[nr].maks_zysk = 0; nr >>= 1; while(nr > 0) { d[nr].suma = d[nr*2].suma + d[nr*2+1].suma; d[nr].ile = d[nr*2].ile + d[nr*2+1].ile; d[nr].maks = d[nr*2].maks + d[nr*2+1].maks; d[nr].maks_pocz = max(d[nr*2].maks_pocz, d[nr*2+1].maks_pocz); d[nr].maks_h = max(d[nr*2].maks_h, d[nr*2+1].maks_h); d[nr].maks_zysk = max(d[nr*2].maks_zysk + d[nr*2+1].ile*d[nr*2].maks_h, d[nr*2+1].maks_zysk + d[nr*2].suma); nr >>= 1; } } int main() { scanf("%d", &n); forr(i, n) { scanf("%lld %lld", &tab[i].h, &tab[i].pocz); } sort(tab, tab+n, comp); S = 1; while(S <= n) S *= 2; forr(i, n) { d[S+i].maks = 1; d[S+i].maks_pocz = tab[i].pocz; d[S+i].maks_h = tab[i].h; d[S+i].maks_zysk = tab[i].pocz; } FORD(i, S-1, 1) { d[i].maks = d[2*i].maks + d[2*i+1].maks; d[i].maks_pocz = max(d[2*i].maks_pocz, d[2*i+1].maks_pocz); d[i].maks_h = max(d[2*i].maks_h, d[2*i+1].maks_h); d[i].maks_zysk = max(d[2*i].maks_zysk, d[2*i+1].maks_zysk); // cout << i << " - maks_pocz: " << d[i].maks_pocz << ", maks_h: " << d[i].maks_h << endl; } long long res = 0; forr(i, n) { the_best = -1; pair<int, long long> best = znajdz_najlepszy(1, 0, 0); wkladaj(best.first); res += best.second; printf("%lld\n", res); //cout << tab[best.first].pocz << " " << tab[best.first].h << endl; } return 0; }
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | // Artur Kraska, II UWr #include <algorithm> #include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <vector> #include <cmath> #include <list> #include <set> #include <map> #define forr(i, n) for(int i=0; i<n; i++) #define FOREACH(iter, coll) for(typeof(coll.begin()) iter = coll.begin(); iter != coll.end(); ++iter) #define FOREACHR(iter, coll) for(typeof(coll.rbegin()) iter = coll.rbegin(); iter != coll.rend(); ++iter) #define lbound(P,R,PRED) ({typeof(P) X=P,RRR=(R), PPP = P; while(PPP<RRR) {X = (PPP+(RRR-PPP)/2); if(PRED) RRR = X; else PPP = X+1;} PPP;}) #define testy() int _tests; scanf("%d", &_tests); FOR(_test, 1, _tests) #define CLEAR(tab) memset(tab, 0, sizeof(tab)) #define CONTAIN(el, coll) (coll.find(el) != coll.end()) #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 deb(X) X; #define M 1000000007 #define INF 1000000007 using namespace std; int n, S; long long the_best; struct polana { long long pocz; long long h; }; polana tab[1000007]; struct drzewo { int ile; int maks; long long maks_pocz; long long maks_h; long long suma; long long maks_zysk; }; drzewo d[2100007]; bool comp(polana a, polana b) { return a.h > b.h; } pair<int, long long> znajdz_najlepszy(int nr, long long suma_l, int ile_p) { if(d[nr].maks == 0) return MP(0, 0); if(nr >= S) { // cout << "element (" << tab[nr-S].pocz << ", " << tab[nr-S].h << ") ma ile: " << ile_p << ", sume: " << suma_l << " i daje wynik " << suma_l + tab[nr-S].pocz + ile_p*tab[nr-S].h << endl; return MP(nr-S, suma_l + tab[nr-S].pocz + ile_p*tab[nr-S].h); } else { pair <int, long long> r = MP(0, 0); long long limit_l = d[nr*2].maks_zysk + suma_l + d[2*nr].maks_h*(d[2*nr+1].ile + ile_p); long long limit_p = d[nr*2+1].maks_zysk + suma_l + d[nr].suma + d[2*nr+1].maks_h*ile_p; if(limit_l > limit_p) { //if(d[nr*2].maks_pocz + (suma_l + d[nr].suma) + d[2*nr].maks_h*(d[nr].ile + ile_p) > the_best) if(limit_l > the_best) { pair <int, long long> l = znajdz_najlepszy(nr*2, suma_l, ile_p + d[nr*2+1].ile); the_best = max(the_best, l.second); r = (r.second > l.second ? r : l); } //if(d[nr*2+1].maks_pocz + (suma_l + d[nr].suma) + d[2*nr+1].maks_h*(d[nr].ile + ile_p) > the_best) if(limit_p > the_best) { pair <int, long long> p = znajdz_najlepszy(nr*2+1, suma_l + d[nr*2].suma, ile_p); the_best = max(the_best, p.second); r = (r.second > p.second ? r : p); } } else { //if(d[nr*2+1].maks_pocz + (suma_l + d[nr].suma) + d[2*nr+1].maks_h*(d[nr].ile + ile_p) > the_best) if(limit_p > the_best) { pair <int, long long> p = znajdz_najlepszy(nr*2+1, suma_l + d[nr*2].suma, ile_p); the_best = max(the_best, p.second); r = (r.second > p.second ? r : p); } //if(d[nr*2].maks_pocz + (suma_l + d[nr].suma) + d[2*nr].maks_h*(d[nr].ile + ile_p) > the_best) if(limit_l > the_best) { pair <int, long long> l = znajdz_najlepszy(nr*2, suma_l, ile_p + d[nr*2+1].ile); the_best = max(the_best, l.second); r = (r.second > l.second ? r : l); } } return r; } } void wkladaj(int nr) { nr += S; d[nr].ile += 1; d[nr].suma += tab[nr-S].h; d[nr].maks = 0; d[nr].maks_pocz = 0; d[nr].maks_h = 0; d[nr].maks_zysk = 0; nr >>= 1; while(nr > 0) { d[nr].suma = d[nr*2].suma + d[nr*2+1].suma; d[nr].ile = d[nr*2].ile + d[nr*2+1].ile; d[nr].maks = d[nr*2].maks + d[nr*2+1].maks; d[nr].maks_pocz = max(d[nr*2].maks_pocz, d[nr*2+1].maks_pocz); d[nr].maks_h = max(d[nr*2].maks_h, d[nr*2+1].maks_h); d[nr].maks_zysk = max(d[nr*2].maks_zysk + d[nr*2+1].ile*d[nr*2].maks_h, d[nr*2+1].maks_zysk + d[nr*2].suma); nr >>= 1; } } int main() { scanf("%d", &n); forr(i, n) { scanf("%lld %lld", &tab[i].h, &tab[i].pocz); } sort(tab, tab+n, comp); S = 1; while(S <= n) S *= 2; forr(i, n) { d[S+i].maks = 1; d[S+i].maks_pocz = tab[i].pocz; d[S+i].maks_h = tab[i].h; d[S+i].maks_zysk = tab[i].pocz; } FORD(i, S-1, 1) { d[i].maks = d[2*i].maks + d[2*i+1].maks; d[i].maks_pocz = max(d[2*i].maks_pocz, d[2*i+1].maks_pocz); d[i].maks_h = max(d[2*i].maks_h, d[2*i+1].maks_h); d[i].maks_zysk = max(d[2*i].maks_zysk, d[2*i+1].maks_zysk); // cout << i << " - maks_pocz: " << d[i].maks_pocz << ", maks_h: " << d[i].maks_h << endl; } long long res = 0; forr(i, n) { the_best = -1; pair<int, long long> best = znajdz_najlepszy(1, 0, 0); wkladaj(best.first); res += best.second; printf("%lld\n", res); //cout << tab[best.first].pocz << " " << tab[best.first].h << endl; } return 0; } |