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

public class sia {

    static long grow(long current, long currentDay, long targetDay, int speed) {
        return current + speed * (targetDay - currentDay);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(); // area
        int m = in.nextInt(); // cut


        int[] speed = new int[n];
        for (int i=0; i<n; i++) {
            speed[i] = in.nextInt();
        }

        long[] grass = new long[n];
        long currentDay = 0;

//        System.out.println(Arrays.toString(grass));
        for (int i=0; i<m; i++) {
            long currentSum = 0;
            long nextDay = in.nextLong();
            long limit = in.nextLong();

            for (int j=0; j<grass.length; j++) {
                grass[j] = grow(grass[j], currentDay, nextDay, speed[j]);
                currentSum += grass[j] - Math.min(grass[j], limit);
                grass[j] = Math.min(grass[j], limit);
            }

            currentDay = nextDay;
            System.out.println(currentSum);
//            System.out.println(Arrays.toString(grass));
        }
    }
}