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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class sia {
    public static void main(String[] args) throws IOException {
    	BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    	StringTokenizer tokenizer;
    	
    	tokenizer = new StringTokenizer(reader.readLine());
    	int n = Integer.parseInt(tokenizer.nextToken());
    	int m = Integer.parseInt(tokenizer.nextToken());

    	tokenizer = new StringTokenizer(reader.readLine());
    	long[] a = new long[n];
    	long[] curr = new long[n];
    	for(int i = 0; i < n; i++) {
    		a[i] = Integer.parseInt(tokenizer.nextToken());
    		curr[i] = 0;
    	}
    	
//    	Arrays.sort(a);
    	
    	long prevD = 0L;
    	for(int i = 0; i < m; i++) {
    		tokenizer = new StringTokenizer(reader.readLine());
    		long d = Integer.parseInt(tokenizer.nextToken());
    		long b = Integer.parseInt(tokenizer.nextToken());
    		
    		long ret = 0;
    		long days = d - prevD;
    		for(int j = 0; j < n; j++) {
    			curr[j] += days * a[j];
    			ret += Math.max(0, curr[j] - b);
    			curr[j] = Math.min(b, curr[j]);
        	}
        	
    		System.out.println(ret);
    		prevD = d;
    	}
    }
}