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
#include <cstdio>
#include <algorithm>

#define PLL pair<long long int, long long int>
#define LL long long int
#define mp make_pair
#define e1 first
#define e2 second

using namespace std;

const int MAXN = 1000010;

PLL g[MAXN];
LL out[MAXN];
LL profit[MAXN];
LL hull[MAXN];

const LL INF = 1234567891234567890LL;

//vector<PLLL> events;

int main()
{
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; i++)
    {
        scanf("%lld%lld", &g[i].e1, &g[i].e2);
    }

    sort(g + 1, g + 1 + n);

    for (int i = 1; i <= n; i++)
    {
        profit[i] = g[i].e2;
    }

    // N KWADRAT
    for (int i = 1; i <= n; i++)
    {
        LL maxid = 0;
        LL maxval = -INF;
        for (int j = 1; j <= n; j++)
        {
            if (profit[j] > maxval)
            {
                maxval = profit[j];
                maxid = j;
            }
        }
        out[i] = out[i - 1] + maxval;
        profit[maxid] = -INF;
        for (int j = 1; j < maxid; j++) profit[j] += g[maxid].e1;
        for (int j = maxid + 1; j <= n; j++) profit[j] += g[j].e1;
    }
    for (int i = 1; i <= n; i++) printf("%lld\n", out[i]);
}