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

long long dl[500001], wg[500001];
int a[500001];

using namespace std;

int main()
{
    int n, m;
    long long d, dost=0, b;
    scanf("%d%d", &n, &m);
    for (int i=0; i<n; i++)
    {
        scanf ("%d", &a[i]);
        dl[i]=0;
    }
    for (int i=0; i<m; i++)
    {
        wg[i]=0;
        scanf ("%lld%lld", &d, &b);
        d-=dost;
        dost+=d;
        for (int j=0; j<n; j++)
        {
            dl[j]+=d*a[j];
            if (dl[j]>b)
            {
                wg[i]+=dl[j]-b;
                dl[j]=b;
            }
        }
    }
    for (int i=0; i<m; i++)
        printf ("%lld\n", wg[i]);
    return 0;
}