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
49
50
51
52
53
54
55
56
57
58
59
60
import java.util.Scanner;

import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;

public class sia {

    private static int[] grassGrowSpeed;
    private static long[] grassLength;
    private static long[] results;

    private static int fieldCount;

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);

        String[] firstLine = scanner.nextLine().split(" ");
        fieldCount = parseInt(firstLine[0]);
        int daysOfCutting = parseInt(firstLine[1]);

        results = new long[daysOfCutting];
        grassLength = new long[fieldCount];
        grassGrowSpeed = new int[fieldCount];

        String[] secondLine = scanner.nextLine().split(" ");
        for (int i = 0; i < fieldCount; i++) {
            int growSpeed = parseInt(secondLine[i]);
            grassGrowSpeed[i] = growSpeed;
        }

        long previousDay = 0;
        for (int i = 0; i < daysOfCutting; i++) {
            String[] line = scanner.nextLine().split(" ");
            long currentDay = parseLong(line[0]);
            long cutHeight = parseLong(line[1]);
            results[i] = getWeight(previousDay, currentDay, cutHeight);
            previousDay = currentDay;
        }

        for (int i = 0; i < daysOfCutting; i++) {
            System.out.println(results[i]);
        }
    }

    private static long getWeight(long prevDay, long currentDay, long height) {
        long weight = 0;

        for (int i = 0; i < fieldCount; i++) {
            long period = currentDay - prevDay;
            long growth = grassGrowSpeed[i] * period;
            grassLength[i] += period * growth;
            if (grassLength[i] > height) {
                weight += grassLength[i] - height;
                grassLength[i] = height;
            }
        }
        return weight;
    }
}