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
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for(int i=(a);i<=(b);++i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define SORT(X) sort(X.begin(),X.end())
#define fi first
#define se second

vector<tuple<long long,long long,int> > Q;
vector<int> S;

long long suf[500500];

int main () {
	int n,mzap;
	scanf("%d%d",&n,&mzap);
	FOR(i,n){
		int a;
		scanf("%d",&a);
		S.pb(a);
	}
	Q.emplace_back( 0,0,0);
	SORT(S);
	for(int i = n-1; i >= 0; i--) suf[i] = suf[i+1]+S[i];
	
	FOR(_,mzap){
		long long d,b;
		scanf("%lld%lld",&d,&b);
		long long ans = 0;
		
		int presuf = n; 
		int m2;
		long long w2,d2;
		tie(d2,w2,m2) = Q.back();
		while(w2 + (d-d2)*S[m2] >= b){
			Q.pop_back();
			ans += (d-d2)*(suf[m2]-suf[presuf]) + (presuf-m2)*(w2-b);
			presuf = m2;
			if(!Q.empty()){
				tie(d2,w2,m2) = Q.back();
			}
			else{
				break;
			}
		}
		if(Q.empty()){
			Q.emplace_back(d,b,0);
		}
		else{
			int p = m2;
			int k = presuf;
			while(p+1 < k){
				int mid = (p+k)/2;
				if(w2+(d-d2)*S[mid] < b) p = mid;
				else k = mid;
			}
			if(k != n){
				ans += (d-d2)*(suf[k]-suf[presuf]) + (presuf - k) * (w2-b);
				Q.emplace_back(d,b,k);
			}
			//printf("k:%d ||", k);
		}

		printf("%lld\n", ans);
	}
}