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
#include <cstdio>
#include <algorithm>
int t[500000],n,m,a,b;
long long int z[500000];


int main() {
	scanf("%d%d", &n, &m);
	for (int i = 0; i < n; i++) {
		scanf("%d", &t[i]);
	}
	std::sort(t, t+n);
	int curday = 0;
	for (int i = 0; i < m; i++) {
		scanf("%d%d", &a, &b);
		long long int wynik = 0;
		for (int x = 0; x < n; x++) {
			z[x] += (((long long int)(a-curday))*t[x]);
			if (z[x] > b) {
				wynik += z[x]-b;
				z[x] = b;
			}
		}
		printf("%lld\n", wynik);
		curday = a;
	}
}