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


public class sia {

	public static void main(String[] args) throws IOException {
		
		sia solver = new sia();
		solver.init();
		solver.solve();
	}
	
	private void init() {
		
	}
	
	private static class Cut {
		int from;
		long d;
		long b;
		
		public Cut(int from, long d, long b) {
			super();
			this.from = from;
			this.d = d;
			this.b = b;
		}
	}
	
	final static Cut CUT_START = new Cut(0,0,0);
	final int RIGHT = 1 << 19;
	
	private Cut[] cutTree = new Cut[RIGHT << 1];
	
	private void addCut(Cut cut) {
		int from = cut.from | RIGHT;
		cutTree[from] = cut;
		while (from > 1) {
			if ((from & 1) == 0) {
				cutTree[from + 1] = cut;
			}
			from >>= 1;
		}
	}
	
	private Cut getLastCut(int field) {
		Cut result = CUT_START;
		field |= RIGHT;
		while (field > 1) {
			if (cutTree[field] != null && cutTree[field].d > result.d) {
				result = cutTree[field];
			}
			field >>= 1;
		}
		
		return result;
	}
	
	
	int N;
	int[] A;
	long[] AS;
	
	private void solve() throws IOException { 
		Reader in = new Reader(System.in);
		PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
		
		N = in.nextInt();
		int M = in.nextInt();
		
		A = new int[N];
		for (int i = 0; i < N; i++) A[i] = in.nextInt();
		Arrays.sort(A);
		
		AS = new long[N];
		AS[0] = A[0];
		for (int i = 1; i < N; i++) AS[i] = AS[i-1] + A[i];
		
		for (int m = 0; m < M; m++) {
			long d = in.nextLong();
			long b = in.nextLong();
			int from = findCutFrom(d,b);
			if (from < N) {
				long result = countCut(from, d, b);
				out.println(result);
				addCut(new Cut(from, d, b));
			} else {
				out.println(0);
			}
			
		}
		
		out.flush();
		out.close();
	}
		
	
	private long countCut(int from, long d, long b) {
		long result = 0;
		int curr = N - 1;
		while (from <= curr) {
			Cut last = getLastCut(curr);
			int ff = Math.max(from, last.from);
			if (ff == 0) {
				result += AS[curr] * (d - last.d) + (curr + 1 - ff) * (last.b - b);
			} else {
				result += (AS[curr] - AS[ff - 1]) * (d - last.d) + (curr + 1 - ff) * (last.b - b);
			}
			
			curr = last.from - 1;
		}
		return result;
	}

	private int findCutFrom(long d, long b) {
		if (!checkCutForField(d,b,N-1)) {
			return N;
		} else {
			int left = 0;
			int right = N-1;
			while (left < right) {
				int mid = (left + right)/2;
				if (checkCutForField(d,b,mid)) {
					right = mid;
				} else {
					left = mid + 1;
				}
			}
			return left;
		}
	}

	private boolean checkCutForField(long d, long b, int field) {
		Cut lastCut = getLastCut(field);
		return lastCut.b + (d - lastCut.d) * A[field] > b;
	}

	private static class Reader {
	    BufferedReader reader;
	    StringTokenizer tokenizer;

	    /** call this method to initialize reader for InputStream */
	    Reader(InputStream input) {
	        reader = new BufferedReader(
	                     new InputStreamReader(input) );
	        tokenizer = new StringTokenizer("");
	    }

	    public void skipLine() throws IOException {
			reader.readLine();
		}

		/** get next word */
	    public String next() throws IOException {
	        while ( ! tokenizer.hasMoreTokens() ) {
	            //TODO add check for eof if necessary
	            tokenizer = new StringTokenizer(
	                   reader.readLine() );
	        }
	        return tokenizer.nextToken();
	    }

	    public int nextInt() throws IOException {
	        return Integer.parseInt( next() );
	    }
	    
	    public double nextDouble() throws IOException {
	        return Double.parseDouble( next() );
	    }
	    
	    public long nextLong() throws IOException {
	    	return Long.parseLong(next());
	    }
	}
}