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
44
45
46
47
48
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class sia {
    public static void main(String[] args) throws IOException {
        BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
        run(input);
    }

    public static void run(BufferedReader input) throws IOException {
        String[] values = input.readLine().split(" ");
        int n = Integer.parseInt(values[0]);
        int m = Integer.parseInt(values[1]);
        int[] growths = new int[n];
        long[] state = new long[n];
        int i = 0;

        for(String s : input.readLine().split(" ")) {
            growths[i] = Integer.parseInt(s);
            ++i;
        }

        long prevDay = 0;
        long day;
        long setting;
        long dayDiff;
        long result;

        for (i = 0; i < m; ++i) {
            result = 0;
            values = input.readLine().split(" ");
            day = Long.parseLong(values[0]);
            setting = Long.parseLong(values[1]);

            dayDiff = day - prevDay;
            for (int p = 0; p < n; ++p) {
                state[p] += dayDiff * growths[p];
                if (state[p] > setting) {
                    result += state[p] - setting;
                    state[p] = setting;
                }
            }
            prevDay = day;
            System.out.println(result);
        }
    }
}