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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import java.util.Scanner;

public class sia {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		int n;
		int m;
		int[] dayCutHeight;
		int[] dayThatGrassIsCutted;
		
		Scanner scanner = new Scanner(System.in);
		
		n = scanner.nextInt();
		m = scanner.nextInt();
		
		dayCutHeight = new int[m];
		dayThatGrassIsCutted = new int[m];
		
		int[] dayHayState = new int[n];
		
		int growthRatio[] = new int[n];
		
		for(int i = 0; i < n; i++){
			growthRatio[i] = scanner.nextInt();
			dayHayState[i] = growthRatio[i];
		}
		
		for(int i = 0; i < m; i++){
			dayThatGrassIsCutted[i] = scanner.nextInt();
			dayCutHeight[i] = scanner.nextInt();
		}
		
		// proceed day cycle
		
		int dailyProfit = 0;
		int dayPointer = 0;
		
		try {
			for (int day = 1; day <= dayThatGrassIsCutted[dayThatGrassIsCutted.length - 1]; day++) {
				if (day == dayThatGrassIsCutted[dayPointer]) { // cut time
					for (int tile = 0; tile < n; tile++) {
						if (dayHayState[tile] - dayCutHeight[dayPointer] > 0) {
							dailyProfit += (dayHayState[tile] - dayCutHeight[dayPointer]);
						} else {
							dailyProfit = 0;
						}

						if (dayHayState[tile] >= dayCutHeight[dayPointer]) {
							dayHayState[tile] = dayCutHeight[dayPointer];
						} else {
							dayHayState[tile] = dayHayState[tile];
						}
					}
					System.out.println(dailyProfit);
					dailyProfit = 0;
					dayPointer++;
				}

				for (int i = 0; i < n; i++) { // daily growth
					dayHayState[i] += growthRatio[i];
				}
			}
		} catch (Exception e) {
			// TODO: handle exception
		}	
	}
}