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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
//Jan Omeljaniuk
//
#include <list>
#include <stack>
#include <queue>
#include <vector>
#include <string>
#include <bitset>
#include <cassert>
#include <iostream>
#include <algorithm>

#define unm unsigned long long int
#define nm long long int
#define uint unsigned int

//#define debug

using namespace std;

struct w_ost{
	unm dzien;
	unm wysokosc;
	w_ost() : dzien(0LL), wysokosc(0LL) {}
	w_ost(unm Dzien, unm Wysokosc) : dzien(Dzien), wysokosc(Wysokosc) {}
};

struct w_zeb{
	//Ile elementow na podprzedziale
	uint ile_el=0LL;
	//Ile wynosi suma wzrostu / dzien elementow na przedziale
	unm smwzr=0LL;
	//Dzien ostatniej aktualizacji
	unm dzakt=0LL;
	//Wysokosc ostatniego przycinania
	unm lwys=0LL;
	//Suma
	unm sm=0LL;
	//Bit aktywnosci
	bool akt=false;
};

unm n,m,base=0LL,int_start,int_koniec;
unm int_dzien, int_wysokosc;
//Lista wartosci wzrostu traw
//(~4 MB)
vector<unm> pole;
vector<unm> wnd;
//Drzewo przechowywujace ostatnie sciecie (ins(przedzial), get(pkt))
//(~16 MB)
vector<w_ost> d_ost;
//Drzewo przechowywujace sume zbiorow na przedziale (ins(przedzial), get(przedzial)) <-- fuck this
//
vector<w_zeb> d_zeb;

unm get_cut(w_zeb temp){
	return (unm)temp.smwzr * (unm)temp.dzakt - ((unm)temp.ile_el) * (unm)temp.lwys;
}

unm int_ins_zeb(uint x, uint start, uint koniec){
	#ifdef debug
		cerr << "int_ins_zeb(" << x << ", " << start << ", " << koniec << ")\n";
	#endif
	// --m | ---
	const uint middle = (start+koniec)/2;

	if(start >= int_start && koniec <= int_koniec){
		d_zeb[x].dzakt = int_dzien;
		d_zeb[x].lwys = int_wysokosc;
		d_zeb[x].sm  = get_cut(d_zeb[x]);
		d_zeb[x].akt = true;
		#ifdef debug
			cerr << "Found whole range, inserting.\n";
			cerr << "Dz: " << d_zeb[x].dzakt << " " << d_zeb[x].lwys << " " << d_zeb[x].sm << " " << d_zeb[x].smwzr << " " << d_zeb[x].ile_el << "\n";
		#endif

		return d_zeb[x].sm;
	}

	if(d_zeb[x].akt&&x*2+1<d_zeb.size()) {

		#ifdef debug
			cerr << "Pushing down\n";
		#endif

		d_zeb[x].akt = false;

		d_zeb[x*2].dzakt = d_zeb[x].dzakt;
		d_zeb[x*2].lwys  = d_zeb[x].lwys;
		d_zeb[x*2].akt   = true;

		d_zeb[x*2].sm	  = get_cut(d_zeb[x*2]);

		d_zeb[x*2+1].dzakt = d_zeb[x].dzakt;
		d_zeb[x*2+1].lwys  = d_zeb[x].lwys;
		d_zeb[x*2+1].akt   = true;

		d_zeb[x*2+1].sm	  = get_cut(d_zeb[x*2+1]);

		d_zeb[x].sm = d_zeb[x*2].sm + d_zeb[x*2+1].sm;
	}

	unm sm = 0;

	if(int_start <= middle&&x*2<d_zeb.size())
		sm += int_ins_zeb(x*2, start, min(middle, koniec));
	else
		sm += d_zeb[x*2].sm;

	if(int_koniec > middle&&x*2<d_zeb.size())
		sm += int_ins_zeb(x*2+1, max(start, middle+1), koniec);
	else
		sm += d_zeb[x*2+1].sm;

	d_zeb[x].sm = sm;

	return sm;

}


inline void ins_zeb(unm start, unm koniec, unm dzien, unm wysokosc){
	#ifdef debug
		cerr << "ins_zeb(" << start << ", " << koniec << ", " << dzien << ", " << wysokosc << ")\n";
	#endif
	int_start = start;
	int_koniec = koniec;
	int_dzien = dzien;
	int_wysokosc = wysokosc;

	int_ins_zeb(1, 0, base/2-1);
}


unm int_get_zeb(uint x, uint start, uint koniec) {

	// --m | ---
	const uint middle = (start+koniec)/2;

	#ifdef debug
		cerr << "int_get_zeb(" << x << ", " << start << ", " << koniec << ") / " << middle << "\n";
	#endif

	if(start >= int_start && koniec <= int_koniec){
		if(d_zeb[x].akt)
			d_zeb[x].sm  = get_cut(d_zeb[x]);
		#ifdef debug
			cerr << "Fully in range(" << d_zeb[x].sm << ")\n";
		#endif
		return d_zeb[x].sm;
	}

	if(d_zeb[x].akt&&x*2+1<d_zeb.size()){
		#ifdef debug
			cerr << "Pushing down\n";
		#endif
		d_zeb[x].akt = false;

		d_zeb[x*2+1].dzakt = d_zeb[x].dzakt;
		d_zeb[x*2+1].lwys = d_zeb[x].lwys;
		d_zeb[x*2+1].akt   = true;
		d_zeb[x*2+1].sm = get_cut(d_zeb[x*2+1]);

		d_zeb[x*2].dzakt = d_zeb[x].dzakt;
		d_zeb[x*2].lwys = d_zeb[x].lwys;
		d_zeb[x*2].akt   = true;
		d_zeb[x*2].sm = get_cut(d_zeb[x*2]);

		d_zeb[x].sm = d_zeb[x*2].sm + d_zeb[x*2+1].sm;

	}

	unm sm = 0LL;

	if(int_start <= middle&&x*2 < d_zeb.size())
		sm += int_get_zeb(x*2, start, min(middle, koniec));

	if(int_koniec > middle&&x*2 < d_zeb.size())
		sm += int_get_zeb(x*2+1, max(start, middle+1), koniec);

	d_zeb[x].sm = sm;

	return sm;


}

inline unm get_zeb(unm start, unm koniec) {
	#ifdef debug
		cerr << "get_zeb(" << start << ", " << koniec << ")\n";
	#endif
	int_start=start;
	int_koniec=koniec;
	const unm temp = int_get_zeb(1, 0, base/2-1);
	#ifdef debug
		cerr << "Result: " << temp << "\n";
	#endif
	return temp;
}

inline w_ost get_ost(unm x) {
	x += base/2;
	w_ost temp = d_ost[x];
	while(x/=2LL)
		if(d_ost[x].dzien>temp.dzien)
			temp = d_ost[x];
	return temp;
}


inline void ins_zeb_pkt(unm x, unm val){
	x+=base/2;
	d_zeb[x].ile_el = 1LL;
	d_zeb[x].akt    = true;
	d_zeb[x].smwzr  = val;
	d_zeb[x].dzakt  = 0LL;
	while(x/=2LL){
		d_zeb[x].dzakt  = 0LL;
		d_zeb[x].ile_el = d_zeb[x*2].ile_el + d_zeb[x*2+1].ile_el;
		d_zeb[x].smwzr  = d_zeb[x*2].smwzr + d_zeb[x*2+1].smwzr;
	}
}

inline void ins_ost(uint start, uint koniec, w_ost val) {

	#ifdef debug_ost
		cout << "ins_ost(" << start << ", " << koniec << ", " << val.dzien << "){\n";
	#endif

	start+=base/2;
	koniec+=base/2;

	d_ost[start]=val;

	if(start!=koniec)
		d_ost[koniec]=val;

	while (start!=1) {

		if(start/2!=koniec/2){
			if(!(start&1))
				d_ost[start+1]=val;
			if(koniec&1)
				d_ost[koniec-1]=val;
		}

		start/=2;
		koniec/=2;
	}

	#ifdef debug_ost
		cout << "}\n";
	#endif

}

bool spr(unm x, unm wysokosc, unm dzien) {

	#ifdef debug_spr
		cerr << "Spr(" << x << ", " << wysokosc << ", " << dzien << ")\n";
	#endif

	w_ost temp = get_ost(x);
	#ifdef debug_spr
		cerr << "pole[" << x << "] = " << pole[x] << ", x.dzien = " << temp.dzien << ", x.wysokosc = " << temp.wysokosc << "\n";
	#endif

	if(wysokosc < pole[x] * (dzien - temp.dzien) + temp.wysokosc ){
		#ifdef debug_spr
			cerr << "HIGH ENOUGH\n";
		#endif
		return true;
	} else {
		#ifdef debug_spr
			cerr << "TOO LOW\n";
		#endif
		return false;
	}
}

int findlastgood(unm wysokosc, unm dzien) {
	#ifdef debug
		cerr << "FindLastGood(" << wysokosc << ", " << dzien << ")\n";
	#endif
	if(!spr(0, wysokosc, dzien)){
		#ifdef debug
			cerr << "NOT FOUND\n";
		#endif
		return -1;
	}

	uint start = 0;
	uint koniec = n-1;
	while(start<koniec){
		const uint cur = (start+koniec+1)/2;
		#ifdef debug_spr
			cerr << "Start: " << start << " Koniec: " << koniec << " Cur: " << cur << "\n";
		#endif
		if(spr(cur, wysokosc, dzien))
			start = cur;
		else
			koniec = cur-1;
	}

	#ifdef debug
		cerr << "Found: " << start  << "\n";
	#endif

	return start;

}

void debug_tree(vector<w_zeb> &drzewo){
	cerr << "Drzewo:\n";
	int k=2*drzewo.size();
	for(nm i=1;i<drzewo.size();i*=2){
		for(int o=0;o<k/2-1;++o)
				cerr << " ";
		for(nm j=i;j<i*2;++j){
			cerr << drzewo[j].smwzr;
			for(int o=0;o<k-1;++o)
				cerr << " ";
		}
		cerr << "\n";
		k/=2;
	}
	cerr << "\n";
}

int main(){

   ios_base::sync_with_stdio(false);

   cin >> n >> m;

	nm base_temp = n;
	while(base_temp/=2LL)
		base++;
	base++;
	base++;
	base = 1LL << base;

	d_ost.resize(base);
	d_zeb.resize(base);


	#ifdef debug
		cerr << "Base: " << base << "\n";
	#endif

   pole.resize(n);
	wnd.resize(n);

   for(unm i = 0 ; i < n ; ++i )
		cin >> pole[i];

	sort(pole.begin(), pole.end(), greater<nm>());

	wnd[0]=pole[0];
	for(unm i =1;i<n;++i)
		wnd[i]=wnd[i-1]+pole[i];

	for(unm i = 0 ; i < n ; ++i )
		ins_zeb_pkt(i, pole[i]);

	nm dzien, wysokosc;

   for( unm i = 0 ; i < m ; ++i ){
		cin >> dzien >> wysokosc;
		const int lastgood = findlastgood(wysokosc, dzien);
		if(lastgood==-1){
			cout << "0\n";
			continue;
		}
		nm zebrano = get_zeb(0, lastgood);

		w_zeb temp;
		temp.dzakt = dzien;
		temp.ile_el = lastgood+1LL;
		temp.lwys = wysokosc;
		temp.smwzr = wnd[lastgood];

		nm wynik = get_cut(temp) - zebrano;
		cout << wynik << "\n";

		ins_zeb(0, lastgood, dzien, wysokosc);
		ins_ost(0, lastgood, w_ost(dzien, wysokosc));
		#ifdef debug
		debug_tree(d_zeb);
		#endif
   }

   return 0;
}