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

using namespace std;

const int MAX_N = 500000;
const int MAX_M = 500000;
long long grass[MAX_N];
long long field[MAX_N];

int main() {
    int n, m;
    scanf("%d%d", &n, &m);
    for (int i = 0; i < n; i++) {
        scanf("%d", &grass[i]);
    }

    long long d_prev = 0;
    for (int i = 0; i < m; i++) {
        long long d, b;
        scanf("%lld%lld", &d, &b);
        long long d_delta = d - d_prev;
        long long weight = 0;
        for (int j = 0; j < n; j++) {
            field[j] += grass[j] * d_delta;
            weight += max(0LL, field[j] - b);
            field[j] = min(field[j], b);
        }
        printf("%lld\n", weight);
        d_prev = d;
    }

    return 0;
}