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

class sia {
	public static void main(String[] args) {
		try (BufferedReader Console = new BufferedReader(new InputStreamReader(System.in))) {
			String[] Input = Console.readLine().split(" ");
			int AreaSurface = Integer.parseInt(Input[0]);
			int CuttingCount = Integer.parseInt(Input[1]);
			long DaySubract = 0;
			long[] Increment = ReadLongArrayFromConsole(Console, AreaSurface);
			long[] FieldState = new long[AreaSurface];
			System.arraycopy(Increment, 0, FieldState, 0, AreaSurface);
			for (int CuttingIndex = 0; CuttingIndex < CuttingCount; CuttingIndex++) {
				long[] CuttingDecription = ReadLongArrayFromConsole(Console, 2);
				long GrassWeigth = 0;
				DaySubract = Math.abs(DaySubract - CuttingDecription[0]);
				for (int GrassItem = 0; GrassItem < AreaSurface; GrassItem++) {
					if (!(FieldState[GrassItem] < CuttingDecription[1])) {
						GrassWeigth = GrassWeigth + (FieldState[GrassItem] - CuttingDecription[1]);
						FieldState[GrassItem] = CuttingDecription[1];
					}
					FieldState[GrassItem] = FieldState[GrassItem] + (Increment[GrassItem] * DaySubract);
				}
				DaySubract = CuttingDecription[0];
				System.out.println(GrassWeigth);
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private static long[] ReadLongArrayFromConsole(BufferedReader ConsoleReader, int Length) throws IOException {
		String[] Input = ConsoleReader.readLine().split(" ");
		long[] ResultArray = new long[Length];
		for (int Index = 0; Index < Length; Index++) {
			ResultArray[Index] = Long.parseLong(Input[Index]);
		}
		return ResultArray;
	}
}