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
#include <iostream>
#include <vector>
#include <algorithm>

int main(void)
{
	int n,m;
	long long d,b,x, xSum;

	std::cin >> n >> m;
	std::vector<int> A(n);
	std::vector<long long> B, D;

	for (int i=0; i < n; i++)
	{
		std::cin >> A[i];
	}

	std::sort(A.rbegin(), A.rend());

	B.assign(n, 0);
	D.assign(n, 0);

	for (int i=0; i<m; i++)
	{
		xSum = 0;

		std::cin >> d >> b;
		for (int j=0; j<n; j++)
		{
			x = B[j] + (d - D[j]) * A[j] - b;

			if (x > 0) {
				xSum += x;
				B[j] = b;
				D[j] = d;
			}
			else {
				break;
			}
		}

		std::cout << xSum << std::endl;
	}

	return 0;
}