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
#include<cstdio>
#include<algorithm>
#include<utility>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>

#define FORE(c, a, b) for(int c=a; c<= int(b); ++c)
#define FORD(c, a, b) for(int c=a; c>= int(b); --c)
#define FORIT(it, cont, cont_t) for(cont_t::iterator it = cont.begin(); it != cont.end(); ++it)
#define REP(i, n) for(int i = 0; i < (n); ++i)
#define ALL(a) a.begin(), a.end() 
#define PR(n) printf("%d ", (int) (n)) 
#define PRL(n) printf("%lld ", (ll) (n)) 
#define SC(n) scanf("%d ", &n) 

#define xx first
#define yy second
#define pb push_back
#define mp make_pair
#define itr iterator

#define dbg if(0)
#define prd dbg printf

using namespace std;

typedef long long ll;
typedef long double ld;
typedef unsigned int uint;

typedef vector<int> vi;
typedef set<int> si;
typedef map<int, int> mi;
typedef pair<int, int> pi;
typedef pair<ll, ll> pll;
typedef vector<pi> vii;
typedef vector<pll> vll;

struct lan {
	int start, stop;
	ll d, b;
};

typedef vector<lan> vlan;

const int NMAX = 500 * 1000 + 9;

int n, m;
int a[NMAX];
ll s[NMAX];
pll q[NMAX];
vlan stos;

bool isCut(ll d1, ll d2, ll b1, ll b2, int vel) {
	return (d2 - d1) * vel >= (b2 - b1);
}

int lastNotCut(lan &last, ll d2, ll b2) {
	int start1 = last.start, stop1 = last.stop - 1;
	while(start1 < stop1) {
		int mid = (start1 + stop1 + 1) / 2;
		if (isCut(last.d, d2, last.b, b2, a[mid]))
			stop1 = mid - 1;
		else
			start1 = mid;
	}
	return start1;
}

ll intervalSum(int i1, int i2) {
	return s[i2] - (i1 ? s[i1 - 1] : 0);
}

void printLan(lan &x) {
	prd("[%d, %d) d = %lld, b = %lld\n", x.start, x.stop, x.d, x.b);
}

int main() {
	stos.reserve(NMAX);
	scanf("%d%d", &n, &m);
	REP(i, n)
		scanf("%d", &a[i]);
	sort(a, a+n);
	REP(i, n)
		s[i] = (i ? s[i-1] : 0) + a[i];
	REP(i, m)
		scanf("%lld%lld", &q[i].xx, &q[i].yy);
	
	lan init;
	init.start = 0, init.stop = n, init.d = 0, init.b = 0;
	stos.pb(init);
	
	REP(i, m) {
		ll suma = 0;
		ll d = q[i].xx, b = q[i].yy;
		
		while(!stos.empty()) {
			lan last = stos.back();
			if(isCut(last.d, d, last.b, b, a[last.start])) {
				suma += intervalSum(last.start, last.stop - 1) * (d - last.d);
				suma -= (b - last.b) * (last.stop - last.start);	
				stos.pop_back();
				//prd("cut, suma = %lld, ", suma); printLan(last); 
			} else break;
		}
		
		int pos = 0;
		if(!stos.empty()) {
			lan last = stos.back();
			pos = lastNotCut(last, d, b) + 1;	
			stos.back().stop = pos;
			suma += intervalSum(pos, last.stop - 1) * (d - last.d);
			suma -= (b - last.b) * (last.stop - pos);
			//prd("updated, suma = %lld, ", suma); printLan(last); 
		}
		
		if(pos < n) {
			lan init;
			init.start = pos, init.stop = n, init.d = d, init.b = b;
			stos.pb(init);
			//prd("created, "); printLan(init); 
		}
		printf("%lld\n", suma);
	}
	
	return 0;
}