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
38
39
40
41
42
43
#include <vector>
#include <cstdint>
#include <iostream>

int main() {
    uint32_t n,m;
    std::cin >> n >> m;
    
    std::vector<uint64_t> speed(n);
    for (uint32_t i=0; i<n; ++i) {
        std::cin >> speed[i];
    }
    
    // not required
    // uint64_t totalHay = 0; // lg(500000*500000*(10^12)) > 77 :/
    
    uint64_t lastCut = 0;
    std::vector<uint64_t> grass(n, 0);
    
    // loop over haymaking days
    for (uint32_t i=0; i<m; ++i) {
        uint64_t d,h;
        std::cin >> d >> h;
        
        uint64_t hay = 0; // lg(500000(10^12)) < 59
        uint64_t days = d - lastCut;
        // loop over all parts of the field
        for (uint32_t j=0; j<n; ++j) {
            grass[j] += speed[j]*days;  // grown till now
            if (grass[j] > h) {         // do we reach it
                hay += grass[j] - h;
                grass[j] = h;
            }
        }
        std::cout << hay << std::endl;
        lastCut = d;
    }

    // we do not want this one in this program
    // std::cout << totalHay << std::endl;

    return 0;
}