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
55
#include <stdio.h>

int n;
int m;

long t[200000];
long long res[1000001];

long long calc(int d);

int main()
{	
	scanf("%d %d", &n, &m);

	for (int i = 0; i < n; i++)
		scanf("%d", &t[i]);

	for (int i = 0; i < 1000001; i++)
		res[i] = -1;

	for(int j = 0; j < m; j++)
	{
		int d;
		scanf("%d", &d);

		if (res[d] == -1)
			res[d] = calc(d);

		printf("%lld\n", res[d]);
	}

	return 0;
}

long long calc(int d)
{
	long long min = 0;
	long long result = 0;

	for(int i = 0; i < n; i++)
	{
		min += d;

		if (min > (long long)t[i])
			result += min - (long long)t[i];

		if (result < 0)
			break;

		if (min < (long long)t[i])
			min = (long long)t[i];
	}

	return result;
}