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

using namespace std;

unsigned long long int a[500005];
unsigned long long int tab[500005];

int main(){
    int n, m;
    cin >> n >> m;
    for(int i=0;i<n;i++){
        cin >> a[i];
        tab[i]=0;
    }
    unsigned long long int last=0, act=0, dd=0, h=0;
    for(int z=0;z<m;z++){
        cin >> act >> h;
        dd = act - last;
        last = act;
        unsigned long long int out = 0;
        for(int i=0;i<n;i++){
            tab[i]+=a[i]*dd;
            if(tab[i]>h){
                //cout << out << " " << tab[i] << " " << h << "\n";
                out += tab[i]-h;
                tab[i] = h;
            }
        }
        cout << out << "\n";
    }

    return 0;
}