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
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;

int n,m,a[500005];
long long d,b,tr[500005]={0},data=0,sc=0,wys;

int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        scanf("%d",&a[i]);
    }
    while(m--){
        sc=0;
        scanf("%lld%lld",&d,&b);
        for(int i=0;i<n;i++){
           wys=(d-data)*a[i]+tr[i];
            if(wys>b){
                sc+=wys-b;
                tr[i]=b;
            }else tr[i]+=a[i];
        }
        data=d;
        printf("%lld\n",sc);

    }
}