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
#include <iostream>
using namespace std;

int main () {
    ios_base::sync_with_stdio (false);
	int n,m; cin>>n>>m;
	long long int dob,dpo = 0,b;
	
	long long int res;
	
	long long int *tab; 
    tab = new long long int [n];
	
	long long int *dane; 
    dane = new long long int [n];
	
    long long int *wynik;
	wynik = new long long int [m];
	
	for (int i = 0; i < n; ++i) {
		cin>>dane[i];
		tab[i] = 0;
	}
	for (int i = 0; i < m; ++i) {
		cin>>dob>>b;
		res = 0;
		if (b == 0) {
			for (int j = 0; j < n; ++j) {
				res += tab[j] + dane[j] * (dob - dpo);
				tab[j] = 0;
			}
		}
		else {
			for (int j = 0; j < n; ++j) {
				tab[j] += dane[j] * (dob - dpo);
				if (tab[j] > b) {
					res += tab[j] - b;
					tab[j] = b;
				}
			}
		}
		wynik[i] = res;
		dpo = dob;
	}
	for (int i = 0; i < m; ++i) {
		cout<<wynik[i]<<'\n';
	}
	cin.get(),cin.get();
	return 0;
}