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
import java.util.*;
import static java.util.Arrays.*;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;

import static java.lang.Math.*;

public class sia {
	long pd =0;long pb=0;
	long bCut = -1;
	long[]A;
	long[]SA;
	int rows = 0;
	long[]rowB;
	int[]rowL;
	long[]rowD;
	static long iter =0;
	long getLen(int id, long d) {
			int a = 0;
	        int b =rows;
	        while(b!=a){
	        	iter++;
	            int c = a+(b-a)/2;
	            if(rowL[c]<id){
	                b=c;
	            }else a=c+1;
	        }
	    int ri = a-1;
		if(ri>=0) {
			long res = rowB[ri];
			long dd = d - rowD[ri];
			res += dd * A[id];
			return res;
		} else return A[id]*d;
	}
	
	//incl
	long sum(int a, int b){
		long ret = SA[b];
		if(a>0)ret-=SA[a-1];
		return ret;
	}
	
	void init(long[]A, long[]d, long[]b){
		this.A = A;
		SA = new long[A.length];
		SA[0]=A[0];
		for(int i=1;i<A.length;i++)SA[i]=SA[i-1]+A[i];
		rowD = new long[d.length];
		rowB = new long[d.length];
		rowL = new int[d.length];
		rows = 1;
		rowL[0]=A.length-1;
		rowB[0]=0;
		rowD[0]=0;
	}
	
	long[] doit(long[]A, long[]d, long[]bSizes){
		init(A,d,bSizes);
		long[] res = new long[bSizes.length];
		for(int t =0;t<d.length;t++) {
			int last =-1;
			long cut =0;
			int a = 0;
	        int b = A.length;
	        while(b!=a){
	            int c = a+(b-a)/2;
	            long len = getLen(c, d[t]);
	            if(len<bSizes[t]){
	                b=c;
	            }else a=c+1;
	        }
	        last = a -1;
	        int prevEnd = -1;
			while(rows > 0 && last >-1) {
				int st = prevEnd+1;
				int en;
				if(last >= rowL[rows-1]) {
					en = rowL[rows-1];
				}
				else {
					en = last;
				}
				long rb = rowB[rows-1];
				long dd = d[t]-rowD[rows-1];
				long totVol = sum(st, en)*dd + (en-st+1)*rb;
				long newVol = (en-st+1)*bSizes[t];
				cut+= totVol -newVol;
				if(last>=rowL[rows-1]){
					prevEnd = rowL[rows-1];
					rows--;
				} else break;
			}
			if(last >-1){
				rowL[rows]=last;
				rowB[rows]=bSizes[t];
				rowD[rows]=d[t];
				rows++;
			}
			res[t]=cut;
		}
		return res;
	}
	
	static long[] doitBrut(long[]A, long[]d, long[]b) {
		long[] L = new long[A.length];
		long[]res = new long[d.length];
		long pd = 0;
		for(int t=0;t<d.length;t++) {
			long cut =0;
			for(int i=0;i<L.length;i++){
				L[i]+= (d[t]-pd)*A[i];
				if(L[i]>b[t]){
					cut += L[i]-b[t];
					L[i]=b[t];
				}
			}
			pd = d[t];
			res[t]=cut;
		}
		return res;
	}
	
	static long[] randLong(int n, int max) {
		long[]res = new long[n];
		Random rand = new Random(0);
		for(int i=0;i<n;i++){
			res[i]=1+rand.nextInt(max);
		}
		
		return res;
	}
	
	static long[]reverse(long[] A){
		int a =0;int b = A.length -1;
		while(a<b){
			long t = A[a];A[a]=A[b];A[b]=t;
			a++;b--;
		}
		return A;
	}
	
	public static void main(String[] args) throws Exception{
		long start = System.currentTimeMillis();
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String fl = br.readLine();
		int n = Integer.parseInt(fl.split(" ")[0]);
		int m = Integer.parseInt(fl.split(" ")[1]);
		fl = br.readLine();
		String[]ss = fl.split(" ");
		long[] A = new long[n];
		for(int i=0;i<n;i++)A[i]=Integer.parseInt(ss[i]);
		sort(A);
		reverse(A);
		long[]b = new long[m];
		long[]d = new long[m];
		for(int i=0;i<m;i++){
			fl = br.readLine();
			ss = fl.split(" ");
			d[i]=Long.parseLong(ss[0]);
			b[i]=Long.parseLong(ss[1]);
		}
		System.err.println("read data: "+(System.currentTimeMillis()-start));
//		A = randLong(100000, 1000000);
//		sort(A);
//		reverse(A);
//		b = randLong(20000, 1000000000);
//		d = randLong(20000, 1000000);
//		sort(d);
//		long[] res = new sia().doit(A, d, b);
//		long[] rb = doitBrut(A, d, b);
//		for(int i=0;i<res.length;i++){
//			if(res[i]!=rb[i]) System.out.println("ERROR");
//		}
		long[] res = new sia().doit(A, d, b);
		System.err.println("solved: "+(System.currentTimeMillis()-start));
		StringBuffer rr = new StringBuffer();
		
		for(int i=0;i<res.length;i++){rr.append(res[i]);rr.append("\n");}
		System.out.print(rr);
		System.err.println(System.currentTimeMillis()-start);
		System.err.println(iter + " iterations");
	}
}