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
#include <algorithm>
#include <cstdio>
#include <stack>

struct tv
{
	tv(): time(0), value(0) {}
	tv(const tv &a): time(a.time), value(a.value) {}

	long long time;
	long long value;
};

struct tvg
{
	tvg(): growth(0), t(tv()) {}
	tvg(long long growth, const tv &t): growth(growth), t(t) {}

	long long growth;
	tv t;
};

struct node
{
	node(): growth(0), applied(tv()), collective(tv()) {}

	long long growth;
	tv applied, collective;
};

struct sn
{
	sn(long long start, long long end, long long index, const tv &prevailing): start(start), end(end), index(index), prevailing(prevailing) {}

	long long start, end, index;
	tv prevailing;
};

int a[500000];
node tree[1 << 20];

tvg get(long long start, long long end, long long index, long long sstart, long long send)
{
	if(start > end)
		return tvg();
	if(start == sstart && end == send)
		return tvg(tree[index].growth, tree[index].collective);
	
	long long sstart1 = sstart;
	long long send1 = (sstart + send) / 2;
	long long sstart2 = send1 + 1;
	long long send2 = send;

	tvg t;

	if(start <= send1)
	{
		if(end <= send1)
			t = get(start, end, 2 * index, sstart1, send1);
		else
		{
			tvg t1 = get(start, send1, 2 * index, sstart1, send1);
			tvg t2 = get(sstart2, end, 2 * index + 1, sstart2, send2);

			t.growth = t1.growth + t2.growth;

			if(t1.t.time > t2.t.time)
			{
				t.t = t1.t;
				t.t.value += t2.t.value + (t1.t.time - t2.t.time) * t2.growth;
			}
			else
			{
				t.t = t2.t;
				t.t.value += t1.t.value + (t2.t.time - t1.t.time) * t1.growth;
			}
		}
	}
	else
		t = get(start, end, 2 * index + 1, sstart2, send2);

	if(tree[index].applied.time > t.t.time)
	{
		t.t = tree[index].applied;
		t.t.value *= end - start + 1;
	}

	return t;
}

void set(long long time, long long value, long long start, long long end, long long index, long long sstart, long long send)
{
	if(start > end)
		return;

	if(start == sstart && end == send)
	{
		tree[index].applied.time = time;
		tree[index].applied.value = value;
		tree[index].collective.time = time;
		tree[index].collective.value = value * (end - start + 1);
		return;
	}

	long long sstart1 = sstart;
	long long send1 = (sstart + send) / 2;
	long long sstart2 = send1 + 1;
	long long send2 = send;

	if(start <= send1)
	{
		if(end <= send1)
			set(time, value, start, end, 2 * index, sstart1, send1);
		else
		{
			set(time, value, start, send1, 2 * index, sstart1, send1);
			set(time, value, sstart2, end, 2 * index + 1, sstart2, send2);
		}
	}
	else
		set(time, value, start, end, 2 * index + 1, sstart2, send2);

	if(tree[2 * index].collective.time > tree[2 * index + 1].collective.time)
	{
		tree[index].collective = tree[2 * index].collective;
		tree[index].collective.value += tree[2 * index + 1].collective.value + (tree[2 * index].collective.time - tree[2 * index + 1].collective.time) * tree[2 * index + 1].growth;
	}
	else
	{
		tree[index].collective = tree[2 * index + 1].collective;
		tree[index].collective.value += tree[2 * index].collective.value + (tree[2 * index + 1].collective.time - tree[2 * index].collective.time) * tree[2 * index].growth;
	}
}

int main()
{
	long long n, m;

	scanf("%lld%lld", &n, &m);

	for(long long i = 0; i < n; ++i)
		scanf("%d", &a[i]);

	std::sort(a, a + n);

	long long offset = 1;
	while(offset < n)
		offset *= 2;

	for(long long i = n - 1; i >= 0; --i)
		tree[i + offset].growth = a[i];
	for(long long i = offset - 1; i >= 1; --i)
		tree[i].growth = tree[2 * i].growth + tree[2 * i + 1].growth;

	for(long long i = 0; i < m; ++i)
	{
		long long d, b;

		scanf("%lld%lld", &d, &b);

		std::stack<sn> s;
		s.emplace(0, offset - 1, 1, tree[1].applied);

		long long start = 0, end = n;

		while(start < end)
		{
			long long mid = (start + end) / 2;

			while(mid < s.top().start || mid > s.top().end)
				s.pop();

			while(s.top().start < s.top().end)
			{
				long long smid = (s.top().start + s.top().end) / 2;
				long long sstart, send, sindex;

				if(mid <= smid)
				{
					sstart = s.top().start;
					send = smid;
					sindex = 2 * s.top().index;
				}
				else
				{
					sstart = smid + 1;
					send = s.top().end;
					sindex = 2 * s.top().index + 1;
				}

				if(s.top().prevailing.time > tree[sindex].applied.time)
					s.emplace(sstart, send, sindex, s.top().prevailing);
				else
					s.emplace(sstart, send, sindex, tree[sindex].applied);
			}

			long long value = s.top().prevailing.value + (d - s.top().prevailing.time) * tree[s.top().index].growth;

			if(value <= b)
				start = mid + 1;
			else
				end = mid;
		}

		tvg t = get(start, n - 1, 1, 0, offset - 1);
		long long start_value = t.t.value + (d - t.t.time) * t.growth;
		long long end_value = (n - end) * b;

		printf("%lld\n", start_value - end_value);

		set(d, b, start, n - 1, 1, 0, offset - 1);
	}

	return 0;
}