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
import java.util.Scanner;

/**
 * Created by Asdf on 2015-09-29.
 */
public class sia {
    public static void main(String[] args) {
        //long startTime = System.currentTimeMillis();
        Scanner scanner = new Scanner(System.in);

        int n = scanner.nextInt();
        int m = scanner.nextInt();

        long []height = new long[n];
        int []data = new int[n];
        for (int i = 0; i < n; i++)
            data[i] = scanner.nextInt();

        long lastDay = 0;
        for (int i = 0; i < m; i++) {
            long day = scanner.nextLong();
            long size = scanner.nextLong();

            long result = 0;
            for (int z = 0; z < n; z++) {
                height[z] += data[z] * (day - lastDay);

                if (height[z] > size) {
                    result += height[z] - size;
                    height[z] = size;
                }
            }

            lastDay = day;
            System.out.println(result);
        }

        //long endTime = System.currentTimeMillis();
        //System.err.println(endTime - startTime);
    }
}