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
#include <cstdio>

const int maxN = 500000;

int growth[maxN];
long long height[maxN];

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

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

    long long previous_cut_day = 0;

    while (m--)
    {
        long long cut_day, max_height;
        scanf ("%lld%lld", &cut_day, &max_height);

        long long res = 0;

        for (int i=0; i<n; i++)
        {
            height[i] += ((cut_day - previous_cut_day) * growth[i]);

            if (height[i] <= max_height)
                continue;

            res += (height[i] - max_height);
            height[i] = max_height;
        }

        printf("%lld\n", res);

        previous_cut_day = cut_day;
    }

    return 0;
}