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

int n,m;
int t[500004];
long long k[500004], d[500004],s[500004],sum=0;
int main()
{
    scanf ("%d%d", &n, &m);
    for (int i=1; i<=n; i++)
    scanf ("%d", &t[i]);

for (int i=1; i<=m; i++)
scanf ("%lld%lld", &k[i], &d[i]);

    for (int i=1; i<=m; i++)
    { sum=0;
    for (int j=1; j<=n; j++)
    {
        s[j]=s[j]+((k[i]-k[i-1])*t[j]);
        if (s[j]>d[i])
        {
         sum=sum+(s[j]-d[i]);
         s[j]=d[i];

        }
    }
    printf ("%lld\n", sum);
    }

}