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
//// BRUT
//#include <iostream>
//#include <algorithm>
//#include <vector>
//#include <iterator>
//
//using namespace std;
//
//const int MAX_N = 500001;
//
//long long N,M;
//long long D,B; //day, level
//
//long long A[MAX_N];
//long long h[MAX_N] = {0,};
//long long old_d = 0;
//
//int main() {
//	ios_base::sync_with_stdio(false);
//
//	cin >> N; cin >>M;
//	for (int i = 0; i < N; ++i) {
//		cin >> A[i];
//	}
//
//	for (int i = 0; i < M; ++i) {
//		cin >> D; cin >> B;
//
//		// grow and cut
//		long long v = 0;
//		for (int j = 0; j < N; ++j) {
//			h[j] += A[j] * (D-old_d);
//			if(h[j] > B) {
//				v += h[j] - B;
//				h[j] = B;
//			}
//		}
//		old_d = D;
//		// harvest
//		cout << v << "\n";
//	}
//
//	return 0;
//}

#include <iostream>
#include <algorithm>
#include <vector>
#include <iterator>

using namespace std;

struct Interval {
	Interval(int a_, int b_, long long d_, long long l_) {
		a = a_;
		b = b_;
		d = d_;
		l = l_;
	}
	int a,b;
	long long d;
	long long l;
};

const int MAX_N = 500001;

int N,M;
long long D,B; //day, level

int A[MAX_N];
long long cum[MAX_N+1];
int s; // last pointer
int s2; // new pointer
long long old_b; // last level
long long old_d; //last day
vector<Interval> history;

long long calcHeight(int i) {
	if (i < s) {
		vector<Interval>::reverse_iterator until(history.begin());
		vector<Interval>::reverse_iterator it(history.end());
		while (it != until) {
			if (i < it->b) {
				return A[i]*(D-it->d) + it->l;
			}
			it++;
		}
		cerr << "Did not found entry in history for "<<i<<"\n";
		return 0;
	} else {
		return A[i]*(D-old_d) + old_b;
	}
}

int findHarvest(int a, int b) {
	int count, step, it;
	count = b-a;
	while(count>0) {
		it = a; step=count/2;  it+=step;
		if (calcHeight(it) < B) {
			a = ++it;
			count -= step+1;
		} else {
			count = step;
		}
	}
	return a;
}

int main() {
	ios_base::sync_with_stdio(false);


	cin >> N; cin >>M;
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	sort(A,A+N);
	cum[N]=0;
	for (int i = N-1; i >=0; --i) {
		cum[i] = A[i] + cum[i+1];
	}

	s2 = 0;
	s = 0;
	old_b = 0;
	old_d = 0;
	history.reserve(MAX_N);
	for (int i = 0; i < M; ++i) {
		cin >> D; cin >> B;

		s2 = findHarvest(0,N);

		long long v = 0;
		if (s2 != N) {
			// harvest
			if (s2 <= s) {
				v = cum[s] * (D-old_d) - (B-old_b) * (N-s);

				while (!history.empty()) {
					Interval iv = history.back();
					history.pop_back();
					int beg = max(iv.a, s2);
					v += (cum[beg]-cum[iv.b]) * (D-iv.d) - (B-iv.l) * (iv.b-beg);
//					if (v < 0) {
//						cerr << "blad2 ";
//						cerr << "beg " << beg << "  iv.b" << iv.b << " D" << D << " iv.d" << iv.d <<
//								" B" << B << " iv.l" << iv.l << " | " << (cum[beg]-cum[iv.b]) * (D-iv.d)
//								<< " | " << (B-iv.l) * (iv.b-beg) <<endl;
//
//					}
					if (iv.a <= s2) {
						// finish popping
						if (iv.a != s2) {
							history.push_back(Interval(iv.a, s2, iv.d, iv.l));
						}
						break;
					}
				}
			} else {
				v = cum[s2] * (D-old_d) - (B-old_b) * (N-s2);
				history.push_back(Interval(s, s2, old_d, old_b));
			}
		} else {
			if(history.empty()) {
				history.push_back(Interval(0,N,0,0));
			} else {
				Interval last = history.back();
				if (last.b < N) {
					history.push_back(Interval(last.b,N,old_d,old_b));
				}
			}
		}

		old_d = D;
		old_b = B;
		s = s2;

		cout << v << "\n";
	}

	return 0;
}