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
#include <bits/stdc++.h>
using namespace std;
#define FOR(i,a,b) for(int i=(a); i<(b); ++i)
//#define REP(i,n) FOR(i,1,(n)+1)
typedef vector<int> vi;
#define pb push_back
#define sz size()
typedef pair<int,int> pii;
#define mp make_pair
#define st first
#define nd second
typedef unsigned long long ll;
#define INF 1000000001
//#define VAR(n,v) typeof(v) n=(v)
#define ALL(t) t.begin(),t.end()
#define SC(a) scanf("%lld", &a)
#define GET(a) ll a; SC(a)

#define ISDEBUG 1
#define dprintf(...) if(ISDEBUG) \
{printf("\033[31m"); printf(__VA_ARGS__); printf("\033[0m");}
template <class It> void dptab(It b, It e, const char* f="%d ") {
	if(ISDEBUG) {
		for(It it=b; it!=e; ++it) dprintf(f, *it); dprintf("\n");
}}

struct clearing {
	ll a, b;
	explicit clearing(ll a, ll b) : a(a), b(b) {}
	bool operator<(const clearing &c) const {
		if(a == c.a) return b > c.b;
		else return a < c.a;
	}
};
vector<clearing> clearings;

struct comparator {
	int k;
	explicit comparator(int k) : k(k) {}
	bool operator()(clearing c1, clearing c2) const {
		ll res = (c1.b+k*c1.a)-(c2.b+k*c2.a);
		if(0 == res) {
			if(c1.a != c2.a) return c1.a < c2.a;
			else return c1.b < c2.b;
		} else return res < 0;
	}
};

clearing get_best(int k) {
	comparator compare(k);
	clearing best = clearings[0];
	FOR(i,1,clearings.sz) {
		if(compare(best, clearings[i])) {
			best = clearings[i];
		}
	}
	return best;
}

ll f(const clearing &c, ll k) {
	return k*c.a + c.b;
}

int find_leader(int k, int last_leader) {
	int leader = k-1;
	ll best = f(clearings[leader], k);

	FOR(i,k,last_leader) {
		ll x = f(clearings[i], k);
		if(x >= best) {
			best = x;
			leader = i;
		}
	}
	return leader;
}

int main() {
	GET(k_max);
	int n = k_max;
	FOR(i,0,n) {
		GET(a);
		GET(b);
		clearings.pb(clearing(a, b));
	}

	if(n<=25) {
//		vi bests(
		vector<ll> bests(n+1);
		FOR(i,0,n) {
			sort(ALL(clearings));
			int mask = 1;
			int n_bits = 0;
			do {
				n_bits = 0;
				int w=0;
				int mask_copy = mask;
				vi v_mask(n);
				while(mask_copy) {
					v_mask[w++] = (mask_copy % 2);
					n_bits += (mask_copy % 2);
					mask_copy /=2;
				}
				ll sum = 0;
				ll k = 0;
				FOR(j,0,n) {
					if(v_mask[j]) {
						sum += f(clearings[j], k++);
					}
				}
				bests[n_bits] = max(bests[n_bits], sum);
				++mask;
			} while(n_bits != n);
		}
		FOR(i,1,n+1) {
			printf("%lld\n", bests[i]);
		}
		return 0;
	}

	//int k_min = 0;
	//while(k_min < k_max) {
	//	clearing c = get_best(k_max);
        //
	//	int k = k_max, bound = k_min;
	//	//binary search of k
	//	while(k - bound > 1) {
	//		int m = (k+bound)/2;
	//		if(c == get_best(m)) k = m;
	//		else bound = m;
	//	}
        //
	//}

	sort(ALL(clearings));
	FOR(days,0,k_max) {
		ll sum = 0;
		int k = days;
		int last_leader = n;
		vector<bool> is_chosen(n);
		vector<clearing> chosen;
		while(k > 0) {
			int leader = find_leader(k, last_leader);
			--k;
			is_chosen[leader] = true;
			chosen.pb(clearings[leader]);
			last_leader = leader;
		}
		ll max_b = clearings[0].b;
		int leader = 0;
		FOR(i,1,n) {
			if(!is_chosen[i] && clearings[i].b > max_b) {
				max_b = clearings[i].b;
				leader = i;
			}
		}
		chosen.pb(clearings[leader]);

		sort(ALL(chosen));
		FOR(i,0,chosen.sz) {
			dprintf("leader: %lld %lld   -> * %lld = %lld\n", chosen[i].a, chosen[i].b, i, f(chosen[i], i));
			sum += f(chosen[i], i);
		}

		printf("%lld\n", sum);
	}
	return 0;
}