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
// Po 1 punkt ;-)

#include <bits/stdc++.h>
using namespace std;

int m, n, A[500000];
long long b, d, p, s, T[500000];

int main() {
	scanf("%d %d", &n, &m);
	for (int i = 0; i < n; ++i) {
		scanf("%d", A + i);
	}
	while (m--) {
		scanf("%lld %lld", &d, &b);
		s = 0;
		for (int i = 0; i < n; ++i) {
			T[i] += (d - p) * A[i];
			if (T[i] > b) {
				s += T[i] - b;
				T[i] = b;
			}
		}
		printf("%lld\n", s);
		p = d;
	}
	return 0;
}