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

static const std::size_t MAX_N = 500000;

static std::uint_fast64_t growth[MAX_N];
static std::uint_fast64_t current[MAX_N];

int main()
{
	std::uint_fast32_t n, m;
	std::cin >> n >> m;

	for (decltype(n) i = 0; i < n; ++i)
		std::cin >> growth[i];

	std::uint_fast64_t previous_day = 0;

	for (decltype(m) i = 0; i < m; ++i)
	{
		std::uint_fast64_t day, length;
		std::cin >> day >> length;

		std::uint_fast64_t sum = 0;
		for (decltype(n) j = 0; j < n; ++j)
		{
			std::uint_fast64_t updated = current[j] + growth[j] * (day - previous_day);
			if (updated > length)
			{
				sum += updated - length;
				current[j] = length;
			}
			else
				current[j] = updated;
		}

		std::cout << sum << std::endl;

		previous_day = day;
	}
}