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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Łukasz Proksa

// Potyczki Algorytmiczne 2015
// Task "Siano"
// Solved O(n)*log^2(n) ~ O(180mln)

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
using namespace std;

typedef long long LL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<PII> VPII;

#define LET(k, val) __typeof(val) k = (val)
#define FOR(i, b, e) for(LET(i,b); i <= (e); ++i) 
#define FORD(i, b, e) for(LET(i,b); i >= (e); --i)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define SIZE(c) ((int)(c).size())
#define ALL(c) (c).begin(), (c).end()
#define FOREACH(i, c) for(LET(i,(c).begin()); i != (c).end(); ++i)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define ST first
#define ND second
#define PB push_back
#define MP make_pair
#define COUT(c) {cout<<SIZE(c)<<":{";FOREACH(i,c){cout<<*i<<",";}cout<<"}";} 

static const int MAX_N = 500000; // 500 tyś

template<class T>
T min_pow_2(T x)
{
  T tmp = 1;
  while (tmp < x) {
    tmp *= 2;
  }
  return tmp;
}

struct Plants {
  struct Node {
    LL cut_h, cut_t; // last cut height, last cut time
    int left_cnt, t, prev_end; // amount of plants on left which last cut is higher than mine
    LL daily; // leaf: daily growth of plant 'i'
    Node(int n) : cut_h(0LL), cut_t(0LL), left_cnt(0), t(0),
        daily(0LL), prev_end(n-1)
    {
    }
  };

  vector<Node> tree; // interval tree
  int n, n_2, cuts_cnt; // at least one plant cut

  Plants(int n) : n(n), n_2(min_pow_2(n)), tree(2*min_pow_2(n), Node(n)), cuts_cnt(0)
  {
  }

  struct Plant {
    LL cut_h, cut_t;
    int i; // index
    Plants* plants;

    Plant(int i, Plants* ptr) : i(i), plants(ptr)
    {
      plants->cut_ht(i, cut_h, cut_t);
    }
    LL h(LL t)
    {
      return cut_h + (t - cut_t) * plants->daily(i);
    }
    int left_cnt()
    {
      return plants->left_cnt(i);
    }
    void init(int ii)
    {
      i = ii;
      plants->cut_ht(i, cut_h, cut_t);
    }
  };

  void cut_ht(int i, LL& cut_h, LL& cut_t)
  {
    int pos = i + n_2;
    cut_h = tree[pos].cut_h;
    cut_t = tree[pos].cut_t; // init val in leaf
    pos /= 2;
    while (pos > 0) {
      if (tree[pos].cut_t > cut_t) { // get latest cut of 'i'
        cut_h = tree[pos].cut_h;
        cut_t = tree[pos].cut_t;
      }
      pos /= 2;
    }
  }

  int left_cnt(int i)
  {
    int pos = i + n_2;
    int left_cnt = tree[pos].left_cnt;
    int t = tree[pos].t; // init in leaf
    pos /= 2;
    while (pos > 0) { // get latest update of left_cnt for 'i'
      if (tree[pos].t > t) {
        left_cnt = tree[pos].left_cnt;
        t = tree[pos].t;
      }
      pos /= 2;
    }
    return left_cnt;
  }

  Plant last_plant(LL h, LL t) // last plant with height > h
  {
    int c = n_2 / 2;
    Plant p(0, this);
    while (c > 0) { // bin s(i)
      int check = MIN(p.i + c, n-1);
      Plant tmp(check, this);
      if (tmp.h(t) > h) {
        p = tmp;
      }
      c /= 2;
    }
    // may not exist
    if (p.h(t) <= h) {
      p.i = -1;
    }
    return p; 
  }

  LL cut(LL h, LL t)
  {
    Plant p = last_plant(h, t); // may not exist
    if (p.i < 0) { // nothing to cut

      return 0LL;
    }
    LL hay = 0LL;
    int end = p.i; // last cut plant index
    while (p.i >= 0) { // while cutting
      int equals_cnt = p.i - p.left_cnt() + 1;
      LL daily_group = daily_sum(p.i - equals_cnt + 1, p.i);
      LL days = (t - p.cut_t);
      LL cut_h_diff = (h - p.cut_h);
      hay += daily_group * days - cut_h_diff * equals_cnt;
      int next_i = p.i - equals_cnt;
      if (next_i < 0) {
        break;
      }
      p.init(next_i);
    }
    cuts_cnt++;
    update_cut_ht(end, h, t);
    update_left_cnt(end);

    return hay;
  } 

  void update_cut_ht(int end, LL h, LL t) // update cut_h, cut_t on plants 0..end
  {
    int pos = n_2 + end;
    tree[pos].cut_h = h;
    tree[pos].cut_t = t;
    while (pos > 1) { // while not root
      if (pos % 2 == 1) { // if 'pos' is right son
        tree[pos - 1].cut_h = h;
        tree[pos - 1].cut_t = t;
      }
      pos /= 2;
    }
  }
  
  void update_left_cnt(int end)
  {
    int pos = n_2 + end;
    // I get prev_end
    int prev_end = tree[pos].prev_end;
    LL prev_t = tree[pos].t;
    pos /= 2;
    while (pos > 0) { 
      if (tree[pos].t > prev_t) {
        prev_t = tree[pos].t;
        prev_end = tree[pos].prev_end;
      }
      pos /= 2;
    }
    // end <= prev_end
    // II for cut plants 0..end
    pos = n_2 + end;
    tree[pos].left_cnt = 0;
    tree[pos].t = cuts_cnt;
    tree[pos].prev_end = end;
    while (pos > 1) {
      if (pos % 2 == 1) { // if 'pos' is roght son
        tree[pos - 1].left_cnt = 0;
        tree[pos - 1].t = cuts_cnt;
        tree[pos - 1].prev_end = end;
      }
      pos /= 2;
    }
    // III for not cut plants end+1..prev_end set: left_cnt, t, prev_end
    if (end == prev_end) { // don't need to update (set empty OR no update needed)
      return;
    }
    // end < prev_end
    int pos_i = n_2 + end + 1;  // left
    int pos_j = n_2 + prev_end; // right
    int cnt = end + 1;          // amount
    tree[pos_i].left_cnt = cnt;
    tree[pos_i].t = cuts_cnt;
    tree[pos_i].prev_end = prev_end;
    if (pos_j != pos_i) {
      tree[pos_j].left_cnt = cnt;
      tree[pos_j].t = cuts_cnt;
      tree[pos_j].prev_end = prev_end;
    }
    while (pos_i / 2 != pos_j / 2) {
      if (pos_i % 2 == 0) { // if 'i' is left son
        tree[pos_i + 1].left_cnt = cnt;
        tree[pos_i + 1].t = cuts_cnt;
        tree[pos_i + 1].prev_end = prev_end;
      }
      if (pos_j % 2 == 1) { // if 'j' is right son
        tree[pos_j - 1].left_cnt = cnt;
        tree[pos_j - 1].t = cuts_cnt; 
        tree[pos_j - 1].prev_end = prev_end;
      }
      pos_i /= 2;
      pos_j /= 2; // go up
    }
  }

  LL daily(int i) // daily growth of 'i' plant
  {
    return tree[n_2 + i].daily;
  }

  LL daily_sum(int i, int j) // sum daily growth plants i..j
  {
    int pos_i = n_2 + i;
    int pos_j = n_2 + j;
    LL sum = tree[pos_i].daily;
    if (pos_i != pos_j) {
      sum += tree[pos_j].daily;
    }
    while (pos_i/2 != pos_j/2) { 
      if (pos_i % 2 == 0) { // if 'i' is left son
        sum += tree[pos_i + 1].daily;
      }
      if (pos_j % 2 == 1) { // if 'j' is right son
        sum += tree[pos_j - 1].daily;
      }
      pos_i /= 2;
      pos_j /= 2; // go up
    }
    return sum;
  }

  void init_daily(VI& src)
  {
    REP(i, n) {
      tree[n_2 + i].daily = (LL)src[i]; // leafs
    }
    int pos = n_2 / 2; // first non-leaf level
    while (pos > 0) { // 'pos' is always first node in current level
      REP(i, pos) { // level has 'pos' nodes
        tree[pos + i].daily = tree[2*(pos+i)].daily + tree[2*(pos+i) + 1].daily;
      }
      pos /= 2;
    }
  }
};

bool cmp(int a, int b)
{
  return (a > b);
}

int main()
{
  ios_base::sync_with_stdio(false);

  int n, m;
  cin>>n>>m;
  Plants plants(n);
  VI daily(n);
  REP(i, n) {
    cin>>daily[i];
  }
  sort(ALL(daily), cmp); // plants sorted desc by daily growth
  plants.init_daily(daily);

  REP(i, m) {
    LL h, t;
    cin>>t>>h;
    cout<<plants.cut(h, t)<<endl;
  }

  return 0;
}