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
//codeforces.com/problemset/problem/573/E
//kod oparty jest na rozwiazaniu http://codeforces.com/contest/573/submission/12767462 autora ww. zadania

#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
#define FOR(i,a,b) for(int i=(a);i<=(b);++i)
#define scanf(...) scanf(__VA_ARGS__)?:0
typedef long long int ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef pair <PLL, int> PLLI;
typedef pair <PLL, PLL> PP;
typedef pair <PII, int> PPI;
typedef pair <ll, int> PLI;
typedef unsigned int ui;
const int inf = 1e9+9;
const ll MOD = 1e9+696969;
const long long INF = 1e18+3;
#define maxn 1000100
PLL t[maxn];
inline ll readLL() {
	char zn; ll x = 0;
	while (1) {
		zn = getchar_unlocked();
		if (zn >= '0' && zn <= '9') break;
	}
	while (1) {
		x = (x << 1) + (x << 3) + zn - '0';
		zn = getchar_unlocked();
		if (isspace(zn)) return x;
	}
}

struct line {
	int a, id;
	ll b;
	line(int aa, ll bb, int idd) : a(aa), id(idd), b(bb) {}
	// does (this,B) intersect before (B,C)
	bool before(const line & B, const line & C) {
		// a*x + b = B.a*x + B.b
		// x * (a-B.a) = B.b-b
		// x * q1 = p1
		ll p1 = B.b - b, p2 = C.b - b;
		int q1 = a - B.a, q2 = a - C.a;
		if(q1 < 0) { p1 *= -1; q1 *= -1; }
		if(q2 < 0) { p2 *= -1; q2 *= -1; }
		// x1 = p1/q1, x2 = p2/q2
		return p1*q2 <= p2*q1;
	}
	ll f(int x) { return (ll) a * x + b; }
};
vector <PLL> wektor;
bool porownaj(int aa, int bb) {
	return wektor[aa].first < wektor[bb].first;
}
struct hull {
	vector<PLL> a; // numbers from input
	vector<bool> taken;
	vector<line> w; // hull
	vector<int> order;
	int n;
	int pointer;
	int count; // number of taken elements
	ll sum; // sum of taken elements
	
	
	hull(){}
	hull(vector<PLL> aa) : a(aa) {
		count = sum = 0;
		n = (int) a.size();
		taken.resize(n, false);
		for(int i = 0; i < n; ++i)
			order.push_back(i);
		// we find order only once, in constructor
		wektor = a;
		sort(order.begin(), order.end(), porownaj);
		build();
	}
	void build() { // build hull
		pointer = 0;
		vector<ll> val(n);
		ll suf = sum;
		int cnt = 0;
		for(int i = 0; i < n; ++i) {
			if(taken[i]) {
				suf -= a[i].first;
				++cnt;
			}
			else val[i] = suf + a[i].second + (ll)a[i].first * (cnt + 1);
		}
		vector<line> sorted;
		// linear sort using vector<int> order
		for(int u=0; u<(int)order.size(); ++u) {
			int i = order[u];
			if(!taken[i])
			sorted.push_back(line(a[i].first, val[i], i));
		}
		
		w.clear();
		for(int i=0; i<(int)sorted.size(); ++i) {
			if(!w.empty() && w.back().a == sorted[i].a) {
				if(sorted[i].b <= w.back().b) continue;
				else w.pop_back();
			}
			while((int) w.size() >= 2) {
				line & A = w[(int)w.size()-2];
				line & B = w[(int)w.size()-1];
				if(A.before(B, sorted[i])) break;
				w.pop_back();
			}
			w.push_back(sorted[i]);
		}
	}
	pair<ll, int> best(int before) { // find the best candidate, move pointer
		if(w.empty()) return make_pair(42LL, -1);
		while(pointer <= (int) w.size()-2 && w[pointer].f(before) < w[pointer+1].f(before))
			++pointer;
		assert(pointer < (int) w.size());
		return make_pair(w[pointer].f(before), w[pointer].id);
	}
	void remove(int i) { // mark as taken
		assert(!taken[i]);
		taken[i] = true;
		sum += a[i].first;
		++count;
		build();
	}
} h[1005];

int a[1005*1005];
int bucket[1051005];

int main() {
	int n;
	n=readLL();
	for(int i = 0; i < n; ++i) t[i].first = readLL(), t[i].second = readLL();
	//n = 300000;
	//for (int i=0; i<n; ++i) t[i].first = i + 1, t[i].second = n - i - 1;
	sort(t, t + n);
	for(int i = 0; i < n; ++i) a[i] = t[i].first;
	// divide into Parts with size sqrt(n)
	int s = max(1, int(sqrt(n)));
	for(int i = 0; i < n; i++) bucket[i] = i / s;
	for(int i = 0; i < n; i += s) {
		vector<PLL> w;
		for(int j = i; j < min(n, i+s); ++j)
			w.push_back(make_pair(a[j], t[j].second));
		h[bucket[i]] = hull(w);
	}
	ll output = 0; // max(score)
	ll score = 0; // score for current set/subsequence
	while(true) {
		pair<ll,int> m = make_pair(42LL, -1);
		ll suf = 0; // sum of elements on the right
		int cnt = -1; // how many on the left
		for(int i = 0; i < n; i += s)
			suf += h[bucket[i]].sum;
		for(int i = 0; i < n; i += s) {
			suf -= h[bucket[i]].sum;
			// we ask this Part about max(ax+b) where x = cnt
			pair<ll, int> p = h[bucket[i]].best(cnt);
			p.first += suf; // extra constant
			if(p.second != -1)
				if(m.second == -1 || p.first > m.first)
					m = make_pair(p.first, i+p.second);
			cnt += h[bucket[i]].count;
		}
		if(m.second == -1) break;
		score += m.first;
		output = max(output, score);
		printf("%lld\n", output);
		int i = m.second;
		h[bucket[i]].remove(i-i/s*s);
	}
	
	return 0;
}