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
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class sia {
	
	static boolean totalGreaterEqualMultiplication(long total, long multiplier1, long multiplier2) {
		return total / multiplier1 >= multiplier2;
	}
	
	static BigInteger toBig(long value) {
		return new BigInteger(String.valueOf(value));
	}
	
	public static void main(String[] args) throws IOException {
		InputStream inputStream = System.in;
		PrintStream printStream = System.out;
		String lineSeparator = System.getProperty("line.separator");
		
		Data data = new DataReader(inputStream, lineSeparator).read();
		
		ActionHandler actionHandler = new ActionHandler(data);
		actionHandler.handleActions(printStream, lineSeparator);
	}
	
	static class ActionHandler {
		
		private Data data;
		private BigInteger totalSum = BigInteger.ZERO;
		
		public ActionHandler(Data data) {
			this.data = data;
		}
		
		int findFirstFieldIndexToBeTrimmed(Action action) {
			int maxIndex = data.getFields() - 1;
			if(totalGreaterEqualMultiplication(action.getMaxSize(), data.getGrowth(maxIndex), action.getDay())) {
				return -1;
			}
				
			int minIndex = 0;
			int currentIndex = -1;
			
			while(minIndex < maxIndex) {
				currentIndex = (minIndex + maxIndex) / 2;
				
				if(totalGreaterEqualMultiplication(action.getMaxSize(), data.getGrowth(currentIndex), action.getDay())) {
					minIndex = currentIndex + 1;
				} else {
					maxIndex = currentIndex;
				}
			}
			
			return minIndex;
		}
		
		public void handleActions(PrintStream printStream, String lineSeparator) {
			while(data.hasNextAction()) {
				Action action = data.getNextAction();
				handleNextAction(action, printStream, lineSeparator);
			}
		}
		
		void handleNextAction(Action action, PrintStream printStream, String lineSeparator) {
			int fieldIndex = findFirstFieldIndexToBeTrimmed(action);
			if(fieldIndex == -1) {
				printStream.print("0");
			} else {
				BigInteger maxPotencialArea = toBig(data.getYearFieldArea(fieldIndex)).multiply(toBig(action.getDay()));
						
				BigInteger sum = maxPotencialArea.subtract(
						toBig(action.getMaxSize()).multiply(toBig(data.getFields() - fieldIndex))).subtract(totalSum);
				
				if(sum.signum() > 0) {
					printStream.print(sum);
					totalSum = totalSum.add(sum);
				} else {
					printStream.print("0");
				}
			}
			
			printStream.print(lineSeparator);
		}
	}
	
	static class DataReader {
		
		private SimpleStringParser simpleStringParser;
		private SimpleNumber simpleNumber;
		
		public DataReader(InputStream inputStream, String lineSeparator) throws IOException {
			simpleStringParser = new SimpleStringParser(100000, inputStream);
			simpleNumber = new SimplePositiveInteger(lineSeparator);
		}
		
		public Data read() throws IOException {
			int n = simpleNumber.parseInt(simpleStringParser) - 1;
			int actionsCount = simpleNumber.parseLastInt(simpleStringParser, true) - 1;
			
			int[] growth = new int[n + 1];
			for(int i = 0;i < n;i++) {
				growth[i] = simpleNumber.parseInt(simpleStringParser);
			}
			growth[n] = simpleNumber.parseLastInt(simpleStringParser, true);
			Arrays.sort(growth);
			
			Action[] actions = new Action[actionsCount + 1];
			for(int i = 0;i < actionsCount;i++) {
				actions[i] = new Action(simpleNumber.parseLong(simpleStringParser), simpleNumber.parseLastLong(simpleStringParser, true));
			}
			actions[n] = new Action(simpleNumber.parseLong(simpleStringParser), simpleNumber.parseLastLong(simpleStringParser, false));
			
			return new Data(growth, actions);
		}
	}
	
	static class Data {
		private int[] growth;
		private Action[] actions;
		private int actionsIndex = 0;
		private final Map<Integer, Long> fieldYearAreas = new HashMap<>();
		
		public Data(int[] growth, Action[] actions) {
			this.growth = growth;
			this.actions = actions;
			calculateFieldYearAreas();
		}
		
		void calculateFieldYearAreas() {
			long yearArea = 0;
			for(int i = growth.length - 1;i >= 0;i--) {
				yearArea += growth[i];
				fieldYearAreas.put(i, yearArea);
			}
		}
		
		public long getYearFieldArea(int index) {
			return fieldYearAreas.get(index);
		}
		
		@Override
		public String toString() {
			return "Data [growth=" + Arrays.toString(growth) + ", actions="
					+ Arrays.toString(actions) + ", actionsIndex="
					+ actionsIndex + "]";
		}

		public int getGrowth(int index) {
			return growth[index];
		}
		
		public Action getNextAction() {
			return actions[actionsIndex++];
		}
		
		public boolean hasNextAction() {
			return actionsIndex < actions.length;
		}
		
		public int getFields() {
			return growth.length;
		}
		
	}
	
	static class Action {
		private long day;
		private long maxSize;
		
		public Action(long day, long maxSize) {
			this.day = day;
			this.maxSize = maxSize;
		}
		
		@Override
		public String toString() {
			return "Action [day=" + day + ", maxSize=" + maxSize + "]";
		}
		
		public long getDay() {
			return day;
		}
		
		public long getMaxSize() {
			return maxSize;
		}
	}

	private static abstract class SimpleNumber {
		protected static final int ZERO = '0';
		protected static final int SPACE = ' ';
		protected final String lineSeparator;
		protected final char beginOfLineSeparator;
		
		public SimpleNumber(String lineSeparator) {
			this.lineSeparator = lineSeparator;
			beginOfLineSeparator = lineSeparator.charAt(0);
		}
		
		public int parseInt(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();}
		public int parseLastInt(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();}
		public long parseLong(SimpleStringParser simpleStringParser) throws IOException {throw new UnsupportedOperationException();}
		public long parseLastLong(SimpleStringParser simpleStringParser, boolean hasMore) throws IOException {throw new UnsupportedOperationException();}
	}
	
	private static class SimplePositiveInteger extends SimpleNumber {
		
		public SimplePositiveInteger(String lineSeparator) {
			super(lineSeparator);
		}
		
		@Override
		public int parseInt(final SimpleStringParser simpleStringParser) throws IOException {
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			if(simpleStringParser.array[simpleStringParser.index] == SPACE) {
				++simpleStringParser.index;
			}
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO;
			int value;
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			while((value = simpleStringParser.array[simpleStringParser.index]) != SPACE) {
				integer *= 10;
				integer += value - ZERO;
				++simpleStringParser.index;
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
				}
			}
			return integer;
		}
		
		@Override
		public int parseLastInt(final SimpleStringParser simpleStringParser, final boolean hasMore) throws IOException {
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			if(simpleStringParser.array[simpleStringParser.index] == SPACE) {
				++simpleStringParser.index;
			}
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			int integer = simpleStringParser.array[simpleStringParser.index++] - ZERO;
			int value;
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			while((value = simpleStringParser.array[simpleStringParser.index]) != beginOfLineSeparator) {
				integer *= 10;
				integer += value - ZERO;
				++simpleStringParser.index;
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
				}
			}
			if(hasMore) {
				simpleStringParser.index += lineSeparator.length();
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
					for(int i = 0;i < lineSeparator.length();++i) {
						if(simpleStringParser.array[i] == lineSeparator.charAt(i)) {
							++simpleStringParser.index;
						}
					}
				}
			}
			return integer;
		}
		
		@Override
		public long parseLong(final SimpleStringParser simpleStringParser) throws IOException {
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			if(simpleStringParser.array[simpleStringParser.index] == SPACE) {
				++simpleStringParser.index;
			}
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			long number = simpleStringParser.array[simpleStringParser.index++] - ZERO;
			int value;
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			while((value = simpleStringParser.array[simpleStringParser.index]) != SPACE) {
				number *= 10;
				number += value - ZERO;
				++simpleStringParser.index;
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
				}
			}
			return number;
		}
		
		@Override
		public long parseLastLong(final SimpleStringParser simpleStringParser, final boolean hasMore) throws IOException {
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			if(simpleStringParser.array[simpleStringParser.index] == SPACE) {
				++simpleStringParser.index;
			}
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			long number = simpleStringParser.array[simpleStringParser.index++] - ZERO;
			int value;
			if(simpleStringParser.nextChunkOfDataRequired()) {
				simpleStringParser.nextChunkOfData();
			}
			while((value = simpleStringParser.array[simpleStringParser.index]) != beginOfLineSeparator) {
				number *= 10;
				number += value - ZERO;
				++simpleStringParser.index;
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
				}
			}
			if(hasMore) {
				simpleStringParser.index += lineSeparator.length();
				if(simpleStringParser.nextChunkOfDataRequired()) {
					simpleStringParser.nextChunkOfData();
					for(int i = 0;i < lineSeparator.length();++i) {
						if(simpleStringParser.array[i] == lineSeparator.charAt(i)) {
							++simpleStringParser.index;
						}
					}
				}
			}
			return number;
		}
	}
	
	private static class SimpleStringParser {
		int index = 0;
		final byte[] array;
		int bytesReaded;
		final InputStream inputStream;
		
		public SimpleStringParser(final int arraySize, final InputStream inputStream) throws IOException {
			array = new byte[arraySize];
			this.inputStream = inputStream;
			bytesReaded = inputStream.read(array);
		}
		
		protected final void nextChunkOfData() throws IOException {
			bytesReaded = inputStream.read(array);
			index = 0;
		}
		
		protected final boolean nextChunkOfDataRequired() {
			return bytesReaded <= index;
		}
		
	}
}