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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#include<bits/stdc++.h>
#define ALL(X)        X.begin(),X.end()
#define FOR(I,A,B)    for(int (I) = (A); (I) <= (B); (I)++)
#define FORW(I,A,B)   for(int (I) = (A); (I) < (B);  (I)++)
#define FORD(I,A,B)   for(int (I) = (A); (I) >= (B); (I)--)
#define CLEAR(X)      memset(X,0,sizeof(X))
#define SIZE(X)       int(X.size())
#define CONTAINS(A,X) (A.find(X) != A.end())
#define PB            push_back
#define MP            make_pair
#define X             first
#define Y             second
using namespace std;
template<typename T, typename U> ostream& operator << (ostream& os, const pair<T, U> &_p) { return os << "(" << _p.X << "," << _p.Y << ")"; }
template<typename T> ostream& operator << (ostream &os, const vector<T>& _V) { bool f = true; os << "["; for(auto v: _V) { os << (f ? "" : ",") << v; f = false; } return os << "]"; }
template<typename T> ostream& operator << (ostream &os, const set<T>& _S) { bool f = true; os << "("; for(auto s: _S) { os << (f ? "" : ",") << s; f = false; } return os << ")"; }
template<typename T, typename U> ostream& operator << (ostream &os, const map<T, U>& _M) { return os << set<pair<T, U>>(ALL(_M)); }
typedef signed long long slong;
typedef long double ldouble;
const slong INF = 1000000100;
const ldouble EPS = 1e-9;
const slong inf = INF * INF;

typedef pair<slong,slong> pll;

const int MAXN = 1000042;
slong A[MAXN], B[MAXN];
slong N;

void read_data() {
    scanf("%lld", &N);
    FOR(i,1,N)
    {
        scanf("%lld %lld", &A[i], &B[i]);
    }
}

slong ans[MAXN];

struct tree {
    typedef pll payload;

    struct node {
        payload result;
        slong lazy;
        node() : result(MP(0,0)), lazy(0) {}
    };

    const payload ZERO = MP(inf,inf);

    inline void update(int v, slong delta) {
        T[v].lazy += delta;
    }

    inline void push(int v) {
        if(v >= M) {
		} else {
            T[2*v].lazy += T[v].lazy;
            T[2*v+1].lazy += T[v].lazy;
            T[v].lazy = 0;
		}
    }

    inline payload eval(int v, int s) {
        if(v >= M) {
            return MP(T[v].lazy, T[v].result.Y);
        } else {
            return min(
                MP(T[2*v].lazy + (2*v < M) * T[2*v].result.X, T[2*v].result.Y),
                MP(T[2*v+1].lazy + (2*v+1 < M) * T[2*v+1].result.X, T[2*v+1].result.Y)
            );
        }
    }

    inline payload merge(const payload &x, const payload &y) {
        return min(x,y);
    }

    int M;
    node *T;

    tree(int n) :
        M(1 << (32 - __builtin_clz(n))),
        T(new node[2*M])
        {}

    inline payload query(int a, int b, int l, int r, int v) {
        if(a > r or b < l) return ZERO;
        push(v);
        if(a == l and r == b) {
            return T[v].result = eval(v, r-l+1);
        }
        int mid = (l + r) / 2;
        payload x = query(a, min(b, mid), l, mid, 2*v);
        payload y = query(max(a, mid+1), b, mid+1, r, 2*v+1);
        T[v].result = eval(v, r-l+1);
        return merge(x, y);
    }

    inline void insert(int a, int b, int l, int r, int v, slong delta) {
        if(a > r or b < l) return;
        push(v);
        if(a == l and r == b) {
            update(v, delta);
            push(v);
            T[v].result = eval(v, r-l+1);
            return;
        }
        int mid = (l + r) / 2;
        insert(a, min(b, mid), l, mid, 2*v, delta);
        insert(max(a, mid+1), b, mid+1, r, 2*v+1, delta);
        T[v].result = eval(v, r-l+1);
    }

    inline payload query(int a, int b) {
        return query(a, b, 0, M-1, 1);
    }

    inline void insert(int a, int b, slong delta) {
        insert(a, b, 0, M-1, 1, delta);
    }
};

// to nie dziala :(
void solve_fast()
{
    vector<pair<pll,int> > cur;
    FOR(i,1,N)
    {
        cur.PB(MP(MP(A[i],B[i]),i));
    }
    sort(ALL(cur));
    tree ST(N);
    slong profit = 0, suf = 0;
    vector<int> pos(N+1,0);
    FORD(i,N-1,0)
    {
        int j = cur[i].Y;
        pos[j] = i;
        ST.T[ST.M+i].result.Y = j;
        slong mush = A[j] * i + B[j]; // grzyby
        profit += mush;
        ST.insert(i,i,mush+suf); // koszt usuniecia to liczba grzybow
                                // + zabranie po 1 dniu kolejnym polankom
        suf += A[j];
    }
    ans[N] = profit;
    FORD(k,N-1,1)
    {
        pll nxt = ST.query(0,N-1);
        int x = nxt.Y; // ktorego ziomka wywalamy
        cerr << "wywalam " << x << " kosztem " << nxt.X << endl;
        profit -= nxt.X;
        ans[k] = profit;
        ST.insert(0,pos[x],-A[x]); // x juz nie bedzie przesuniety
        ST.insert(pos[x],pos[x],inf);
    }

    FOR(k,1,N)
    {
        printf("%lld\n", ans[k]);
    }
}

slong calc(vector<pll> v)
{
    sort(ALL(v));
    slong ret = 0;
    FORW(i,0,SIZE(v))
    {
        ret += v[i].Y + i*v[i].X;
    }
    return ret;
}


// O(N^2)
void solve2() {
    vector<pll> cur;
    FOR(i,1,N)
    {
        cur.PB(MP(A[i],B[i]));
    }
    sort(ALL(cur));
    FORD(k,N,1)
    {
        slong profit = 0, suf = 0;
        pll opt = MP(inf,inf);
        FORD(i,k-1,0)
        {
            slong mush = cur[i].X * i + cur[i].Y; // grzyby
            profit += mush;
            pll temp = MP(mush+suf,i);
            opt = min(opt, temp);
            suf += cur[i].X;
        }
        ans[k] = profit;

        //cerr << "wywalam " << cur[opt.Y] << " kosztem " << opt.X << endl;
        cur.erase(cur.begin()+opt.Y);
    }
    FOR(k,1,N)
    {
        printf("%lld\n", ans[k]);
    }
}

void solve() {
    vector<int> used(N+1);
    FOR(i,0,N) used[i] = i;
    FORD(k,N,1)
    {
        slong best = 0, ii = 0;
        FORW(j,0,SIZE(used))
        {
            int x = used[j];
            vector<pll> cur;
            for(int y : used)
            {
                if(y != x) cur.PB(MP(A[y],B[y]));
            }
            slong temp = calc(cur);
            if(temp > best)
            {
                best = temp;
                ii = j;
            }
        }
        used.erase(used.begin()+ii);
        ans[k] = best;
    }
    FOR(k,1,N)
    {
        //printf("%lld\n", ans[k]);
    }
}

void solve_forward() {
    vector<pll> cur;
    vector<int> used(N+1,0);
    FOR(k,1,N)
    {
        cur.PB(MP(0,0));
        slong best = 0, ii = 0;
        FOR(i,1,N)
        {
            if(used[i]) continue;
            cur[k-1] = MP(A[i],B[i]);
            slong temp = calc(cur);
            if(temp > best)
            {
                best = temp;
                ii = i;
            }
        }
        cur[k-1] = MP(A[ii],B[ii]);
        used[ii] = 1;
        ans[k] = best;
    }
    FOR(k,1,N)
    {
        //printf("%lld\n", ans[k]);
    }
}

void gen_data()
{
    N = rand()%10 + 1;
    int M = 1000000;
    FOR(i,1,N)
    {
        A[i] = rand()%M;
        B[i] = rand() * (rand() % 1000) + rand();
    }
}

slong ans_brut[MAXN];
void brut()
{
    //CLEAR(ans_brut);
    FOR(i,1,N) ans_brut[i] = 0;
    FORW(mask,1,(1<<N))
    {
        vector<pll> cur;
        FOR(i,1,N)
        {
            if(!(mask & (1<<(i-1)))) continue;
            cur.PB(MP(A[i],B[i]));
        }
        int k = __builtin_popcount(mask);
        //cerr << k << " " << mask << " " << cur << " " << calc(cur) << endl;
        ans_brut[k] = max(ans_brut[k], calc(cur));
    }
}

int main() {
    srand(time(0));
    int cnt = 0;
    while(0)
    {
        cnt++;
        if(cnt%100 == 0) cerr << "." << flush;
        gen_data();
        solve2();
        brut();
        FOR(k,1,N)
        {
            if(ans[k] != ans_brut[k])
            {
                printf("\n%d\n", N);
                FOR(i,1,N) printf("%lld %lld\n", A[i], B[i]);
                cerr << "dla k: " << k << "  ans:" << ans[k] << "  a brut " << ans_brut[k] << endl;
                return 0;
            }
        }
    }
    read_data();
    solve2();
}