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
#include<cstdio>
#include<deque>
#include<algorithm>
std::deque<std::pair<long long, long long> > d;
std::deque<std::pair<long long, long long> >:: iterator it,akt;
std::pair<long long, long long>g[1000002], pom;
int n, num=0;
long long sum,a,b, maks=0, il=0,wynik=0;
int main()
{
	scanf("%d", &n);
	for(int i=0; i<n; i++)
	{
		scanf("%lld%lld", &b, &a);
		g[i].first=b; g[i].second=a;
	}
	std::sort(g, g+n);
	for(int i=0; i<n; i++)
	{
		if(g[i].second>maks)
		{
			maks=g[i].second;
			il=g[i].first;
			num=i;
		}
		else if(g[i].second==maks&&g[i].first>il)
		{
			il=g[i].first;
			num=i;
		}
	}
		pom.first=g[num].first;
		pom.second=g[num].second;
		sum=il;
		g[num].first=0;
		g[num].second=0;
		d.push_back(pom);
		pom.first=(long long)10000000000000;
		d.push_back(pom);
		printf("%lld\n", maks);
		for(int i=1; i<n; i++)
		{
			il=0;
			it=d.begin();
			maks+=sum;
			wynik=maks;
			for(int j=0; j<n; j++)
			{
				while((*it).first<g[j].first)
				{
					wynik-=(*it).first; il++; it++;
				}
				if(wynik+g[j].second+g[j].first*il>maks)
				{
					num=j;
					maks=wynik+g[j].second+g[j].first*il;
					akt=it;
				}
			}
			sum+=g[num].first;
			pom.first=g[num].first;
			pom.second=g[num].second;
			d.insert(akt, pom);
			g[num].first=0;
			g[num].second=0;
			printf("%lld\n", maks);
		}	
		return 0;
}