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
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

namespace {

struct Mushroom {
  int a;
  long long b;

  Mushroom(int a_, long long b_): a{a_}, b{b_}
  {
  }
};

long long select(long long& total, vector<Mushroom>& data)
{
  int pos = -1;
  long long res = 0;
  int n = data.size();
  int count = 0;
  long long sum = total;
  for (int i = 0; i < n; ++i) {
    if (data[i].b < 0) {
      ++count;
      sum -= data[i].a;
      continue;
    }
    long long cur = static_cast<long long>(data[i].a) * count + data[i].b + sum;
    if (pos < 0 || cur > res) {
      res = cur;
      pos = i;
    }
  }
  total += data[pos].a;
  data[pos].b = -1;
  return res;
}

}

int main()
{
  iostream::sync_with_stdio(false);
  cin.tie(nullptr);

  int n;
  cin >> n;

  vector<Mushroom> data;
  data.reserve(n);
  for (int i = 0; i < n; ++i) {
    int a;
    long long b;
    cin >> a >> b;
    data.emplace_back(a, b);
  }

  sort(data.begin(), data.end(), [](Mushroom const& lhs, Mushroom const& rhs) { return lhs.a < rhs.a; });

  long long cur = 0;
  long long total = 0;
  for (int k = 1; k <= n; ++k) {
    cur += select(total, data);
    cout << cur << '\n';
  }

  return 0;
}