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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <algorithm>
#include <cassert>
#include <cstdio>
#include <vector>
using namespace std;

using ll = long long;
using pll = pair<ll, ll>;

// z biblioteczki UJ
namespace kody {
using node_size_t = int; //suma wszystkich node_size() powinna sie miescic!

using T = pair<ll, pll>;

struct SplayTree {
#define _subtree_size(x) ((x)?(x)->subtree : 0)
    struct Node{
        T val;

        Node* left_, *right_, *p_; //tych (i dalszych) pol nie dotykac bezposrednio
        Node(const T& v):
                val(v), left_(nullptr), right_(nullptr), p_(nullptr){
            resize();
        }
        void set_left(Node *x){
            push();
            left_ = x; if(x) x->p_ = this; resize(); }
        void set_right(Node *x){
            push();
            right_ = x; if(x) x->p_ = this; resize();}

        //template<> node_size_t SplayTree<KTH_TEST>::Node::node_size(){ ... }
        node_size_t node_size(){return 1;}
        node_size_t subtree;

        void resize(){
            subtree = _subtree_size(left_) + _subtree_size(right_) + node_size();
        }

        void push() {
            if (left_) {
              left_->val.second.first += val.second.first;
              left_->val.second.second += val.second.second;
            } else {
              val.first += val.second.second;
            }
            if (right_) {
              right_->val.second.first += val.second.first;
              // right_->val.second.second += val.second.second + val.second.first * (_subtree_size(left_) + 1);
            }
            val.first += val.second.first;
            val.second = {0, 0};
        }

        void rotate() {
            Node *parent = p_;
            this->p_ = parent->p_;
            if(parent->p_) {
                if (parent==parent->p_->left_) parent->p_->set_left(this);
                else parent->p_->set_right(this);
            }

            if (this==parent->left_) {
                parent->set_left(this->right_);
                set_right(parent);
            } else {
                parent->set_right(this->left_);
                set_left(parent);
            }
        }

        void dump_inorder_(vector<T>&out){              /* DUMP */
            push();                                     /* DUMP */
            if(left_) left_->dump_inorder_(out);        /* DUMP */
            out.push_back(val);                         /* DUMP */
            if(right_) right_->dump_inorder_(out);      /* DUMP */
        }                                               /* DUMP */

        void clear_(){                                      /* CLEAR */
            if(left_) {left_->clear_(); delete left_;}      /* CLEAR */
            if(right_) {right_->clear_(); delete right_;}   /* CLEAR */
        }                                                   /* CLEAR */
    } *root;

    void dump_inorder(vector<T>&out){           /* DUMP */
        if(root) root->dump_inorder_(out);      /* DUMP */
    }                                           /* DUMP */

    SplayTree():root(nullptr){}
    //UWAGA - piszac deepcopy pamietaj o pushowaniu
    ~SplayTree(){ clear(); }    /* CLEAR */

    void clear(){           /* CLEAR */
        if(!root) return;   /* CLEAR */
        root->clear_();     /* CLEAR */
        delete root;        /* CLEAR */
        root = nullptr;        /* CLEAR */
    }

    Node* splay(Node *v) { //uwaga, zmienia korzen drzewa
        while(v->p_) {
            if (v->p_->p_) v->p_->p_->push();
            v->p_->push();
            v->push();
            if (v->p_->p_ && ((v==v->p_->left_) == (v->p_==v->p_->p_->left_)))
                v->p_->rotate();
            v->rotate();
        }
        return root = v;
    }

    /* MULTISET */

    Node* lower_bound(const T &x){ //zmienia korzen, NIEKONIECZNIE na wynik!
        Node *v = root, *res = nullptr, *prev = nullptr;
        while(v){
            prev = v;
            if(v->val < x) v = v->right_;
            else {
                res = v;
                v = v->left_;
            }
        }
        if(prev) splay(prev); //XXX czy na pewno tak chcemy ?
        return res;
    }

    /* PATH */

    // Pierwszy wierzcholek taki ze suma rozmiarow wierzcholkow w porzadku
    // inorder do tego wierzcholka wlacznie jest > k. Jesli rozmiary sa = 1,
    // to jest to k-ty wierzcholek w porzadku inorder. (Liczac od 0)
    Node *splay_kth(node_size_t k){
        Node *v = root;
        if (_subtree_size(v) <= k) return nullptr;
        for(;;) {
            if(!v) return nullptr;
            v->push();
            if (_subtree_size(v->left_) <= k &&
                    _subtree_size(v->left_) + v->node_size()>k){
                return splay(v);
            }
            if (_subtree_size(v->left_) <= k) {
                k -= (v->node_size()+_subtree_size(v->left_));
                v = v->right_;
            } else v = v->left_;
        }
    }

    void append_(Node* nw){
        if(!nw) return;
        Node *v = root;
        while(v){
            v->push();
            if(v->right_) v = v->right_;
            else break;
        }

        if(v) v->set_right(nw);
        splay(nw);
    }

    void append(const T& val){
      append_(new Node(val));
    }

    void insert_at(node_size_t k, const T& val){ //val bedzie na ktej pozycji
        // (od zera). UWAGA - Jesli k jest duze, to val bedzie appendowane.
        // Jesli rozmiary wezlow nie sa jednostkowe, to val zostanie wsadzone
        // na najdalsza pozycje taka, ze suma elementow przed nia jest <= k
        if(!root){
            append(val);
            return;
        }
        SplayTree st = split_from(k);
        append(val);
        extend(st);
    }

    SplayTree split_from(node_size_t k){ //odrywa podsciezke o ind. [k, k+1, ...]
        SplayTree res;
        if(!splay_kth(k)) return res;

        Node *left_ = root->left_;
        root->set_left(nullptr);
        root->resize();
        if(left_) left_->p_ = nullptr;

        res.root = root;
        root = left_;

        return res;
    }

    void extend(SplayTree &rh){ //wchlania sciezke rh (dokleja na koniec)
        assert(this != &rh);
        if(!rh.root) return;
        if(!root) root = rh.root;
        else append_(rh.root);
        rh.root = nullptr;
    }

    void add(ll a, ll b) {
      root->val.second.first += a;
      root->val.second.second += b;
    }

    int lookup(ll a, ll b) {
      if (_subtree_size(root) == 1) return 1;

      int ret = _subtree_size(root);
      int cnt = 0;
      Node *n = root, *last;
      while (n) {
        last = n;
        n->push();
        ll val = n->val.first;
        int cand = cnt + _subtree_size(n->left_);
        if (cand && a * (cand-1) + b >= val) {
          n = n->left_;
          ret = cand;
        }
        else {
          cnt += _subtree_size(n->left_) + 1;
          n = n->right_;
        }
      }
      splay(last);
      return ret;
    }
};
}

kody::SplayTree res;

void update(int from, ll a, ll b) {
  // printf("from %d to %d: %lld %lld\n", from, to, a, b);
  res.insert_at(from, {0, {0, 0}});

  auto suf = res.split_from(from);
  suf.add(a, b + a * (from-1));
  suf.splay_kth(0)->val.first -= a;
  res.extend(suf);
}

int main() {
  int n;
  scanf("%d", &n);
  // n = 1000000;
  vector<pll> v(n);
  for (auto& i : v) {
    // i.first = rand() % 1000000;
    // i.second = (ll)(rand() % 1000000) * (rand() % 1000000);
    scanf("%lld %lld", &i.first, &i.second);
  }
  sort(v.begin(), v.end());
  res.append({0, {0, 0}});
  for (int i=0; i<n; i++) {
    int from = res.lookup(v[i].first, v[i].second);
    // printf("add %d(%d) %d %lld %lld\n", hi, from, i+1, v[i].first, v[i].second);
    update(from, v[i].first, v[i].second);
    if(0) {
      vector<pair<ll,pll>> out;
      out.reserve(n+1);
      res.dump_inorder(out);
      for (auto t : out) printf("%lld ", t.first); printf("\n");
    }
  }
  vector<pair<ll,pll>> out;
  out.reserve(n+1);
  res.dump_inorder(out);
  ll sum = 0;
  for (int i=1; i<=n; i++) {
    sum += out[i].first;
    printf("%lld\n", sum);
  }

  return 0;
}