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

public class sia {

	public static void main(String args[]) throws IOException {
		String line;
		 BufferedReader bufferedReader = new BufferedReader(new
		 InputStreamReader(System.in));
		//BufferedReader bufferedReader = new BufferedReader(new FileReader("C:\\Users\\ynka\\Downloads\\sia_big\\sia_big.in"));
		// read m and n
		line = bufferedReader.readLine();
		String[] arr = line.split("\\s");
		int n_arow = Integer.parseInt(arr[0]);
		int m_koszen = Integer.parseInt(arr[1]);

		// read a
		int[] a = new int[n_arow];
		long[] h = new long[n_arow];
		line = bufferedReader.readLine();
		arr = line.split("\\s");
		for (int i = 0; i < n_arow; i++) {
			a[i] = Integer.parseInt(arr[i]);
		}

		long previous_day = 0;
		// read d and b
		for (int i = 0; i < m_koszen; i++) {
			line = bufferedReader.readLine();
			arr = line.split("\\s");
			long d = Long.parseLong(arr[0]);
			long b = Long.parseLong(arr[1]);			
			
			//do one case
			long gain = 0;
			for (int j = 0; j < n_arow; j++) {
				h[j] += (d - previous_day) * a[j];
				gain += Math.max(h[j] - b, 0);
				h[j] = Math.min(h[j], b);
			}
			System.out.println(gain);
			previous_day = d;
			
		}
		bufferedReader.close();		
	}

}