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

using namespace std;

int main()
{
	std::ios_base::sync_with_stdio(false);
	cin.tie(NULL);

	uint32_t n,m;
	cin >> n >> m;

	vector<uint32_t> a(n);
	vector<uint64_t> h(n);
	vector<uint64_t> d(n);

	for (uint32_t i=0; i<n; i++)
		cin >> a[i];

	sort(a.begin(), a.end(), greater<uint32_t>());

	uint64_t d_curr, b;

	for (uint32_t k=0; k<m; k++)
	{
		cin >> d_curr >> b;

		uint64_t daysum = 0;

		for (uint32_t i=0; i<n; i++)
		{
			uint64_t d_diff = d_curr - d[i];
			h[i] += d_diff * a[i];
			d[i] = d_curr;
			int64_t cut = h[i] - b;
			if (cut > 0)
			{
				daysum += cut;
				h[i] = b;
			}
			else
			{
				break;
			}
		}
		cout << daysum << endl;
	}

	return 0;
}