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

long long wys[500000] = {0}, last[500000] = {0};
long long d, b, ld = 0;
int n, m;

// 4 4   1 2 4 3   1 1   2 2   3 0   4 4

int main(int argc, const char * argv[]){
    
    scanf("%d %d", &n, &m);
    for (int i = 0; i < n; ++i) scanf("%lld", &(wys[i]));
    
    for (int j = 0; j < m; ++j) {
        scanf("%lld %lld", &d, &b);
        long long res = 0;
        for (int i = 0; i < n; ++i) {
            long long w = last[i] + (d - ld) * wys[i];
            res += w > b ? w - b : 0;
            last[i] = w > b ? b : w;
        }
        ld = d;
        printf("%lld\n", res);
    }
    return 0;
}