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
#include <algorithm>
#include <cstdio>
#include <stack>
using namespace std;

#define FOR(i,a,b) for(int i=(a);i<(b);++i)
#define REP(i,n) FOR(i,0,n)

typedef long long LL;

int n, m;
LL a[500000], g[500001];

struct E {
	E(int i, LL d, LL b) : i(i), d(d), b(b) {}
	int i;
	LL d, b;
};

inline LL get(const E& e, int i, LL d) {
	return e.b + (d - e.d) * a[i];
}

inline LL getRange(const E& e, int i, int j, LL d) {
	return e.b * (j - i) + (d - e.d) * (g[j] - g[i]);
}

int main() {
	scanf("%d%d", &n, &m);
	REP(i,n) scanf("%lld", &a[i]);
	sort(a, a + n);
	g[0] = 0;
	REP(i,n) g[i + 1] = g[i] + a[i];
	stack<E> s;
	s.push(E(0, 0, 0));
	REP(j,m) {
		LL d, b;
		scanf("%lld%lld", &d, &b);
		int last = n;
		LL c = 0;
		while (!s.empty() && get(s.top(), s.top().i, d) >= b) {
			c += getRange(s.top(), s.top().i, last, d) - b * (last - s.top().i);
			last = s.top().i;
			s.pop();
		}
		int y = last;
		if (!s.empty()) {
			int x = s.top().i;
			while (x + 1 < y) {
				int h = (x + y) >> 1;
				if (get(s.top(), h, d) < b) x = h;
				else y = h;
			}
			c += getRange(s.top(), y, last, d) - b * (last - y);
		}
		if (y < n) s.push(E(y, d, b));
		printf("%lld\n", c);
	}
}