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
//============================================================================
// Name        : PA2015_1A_Siano.cpp
// Author      : Kornel
// Description : Siano
//============================================================================

#include <stdio.h>
using namespace std;

#define MAX 500000

int N;
int M;
int grownSpeed[MAX];
long long harvestDay[MAX];
long long harvestHeight[MAX];
int speed;
long long day;
long long height;
long long lastHarvestDay = 0;
long long totalSpeed;
long long heightArea[MAX];

long long brutal(long long day, long long height) {
	long long total = 0;
	for (int i=0; i<N; i++) {
		long long currentHeight = heightArea[i] + (day - lastHarvestDay) * grownSpeed[i];
		if (currentHeight > height) {
			total += (currentHeight - height);
			heightArea[i] = height;
		} else {
			heightArea[i] = currentHeight;
		}
	}
	lastHarvestDay = day;
	return total;
}

int main() {
	scanf("%d %d", &N, &M);
	for (int i=0; i<N; i++) {
		scanf("%d", &speed);
		grownSpeed[i] = speed;
		totalSpeed += speed;
	}
	for (int i=0; i<M; i++) {
		scanf("%lld %lld", &day, &height);
		harvestDay[i] = day;
		harvestHeight[i] = height;
		printf("%lld\n", brutal(day, height));
	}
	return 0;
}