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
// Grzegorz Bukowiec
// PA 2015 - Zadanie 1A - Siano

#include <iostream>
#include <algorithm>
#include <vector>
#include <stack>
using namespace std;

struct Struct {
	long long end;
	long long height;
	long long times;
	long long start;

	Struct(long long end, long long height, long long times, long long start) {
		this->end = end;
		this->height = height;
		this->times = times;
		this->start = start;
	}
};


vector<long long> field;
long long n, m, a, border;
long long d, b, d2, b2, result, height, quantity, times, start;
vector<long long> suffixSum;
stack<Struct> backwards;
bool pushBack;

long long binsearch(long long beg, long long end) {
	if (beg >= end) {
		return beg;
	}

	if (b2 + field[(beg + end) / 2] * (d - d2) >= b) {
		return binsearch(beg, (beg + end) / 2);
	} else {
		return binsearch((beg + end) / 2 + 1, end);
	}
}

long long binsearch2(long long beg, long long end) {
	if (beg >= end) {
		return beg;
	}

	if (height + field[(beg + end) / 2] * (d - d2 + times) >= b) {
		return binsearch2(beg, (beg + end) / 2);
	} else {
		return binsearch2((beg + end) / 2 + 1, end);
	}
}

void increaseTimes(long long inc) {
	long long end = backwards.top().end;
	start = backwards.top().start;
	height = backwards.top().height;
	times = backwards.top().times;
	backwards.pop();
	backwards.push(Struct(end, height, times + inc, start));
}

int main() {
	ios_base::sync_with_stdio(0);
	border = 0;
	d2 = 0;
	b2 = 0;

	cin >> n;
	cin >> m;

	for (long long i = 0; i < n; ++i) {
		cin >> a;
		field.push_back(a);
	}

	suffixSum.push_back(0);
	sort(field.begin(), field.end());

	for (long long i = n - 1; i >= 0; --i) {
		suffixSum.push_back(field[i] + (suffixSum.empty() ? 0 : suffixSum[suffixSum.size() - 1]));
	}
	reverse(suffixSum.begin(), suffixSum.end());

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

		if (border < n && b2 + field[border] * (d - d2) < b) {
			border = binsearch(border, n);
			result = suffixSum[border] * (d - d2) + (b2 - b) * (n - border);
			if (border > 0) {
				if (backwards.empty() || border - 1 > backwards.top().end) {
					backwards.push(Struct(border - 1, b2, (d - d2), backwards.empty() ? 0 : backwards.top().end + 1));
				} else {
					increaseTimes(d - d2);
				}
			}
		} else {
			times = 0;
			result = border == n ? 0 : suffixSum[border] * (d - d2) + (b2 - b) * (n - border);
			while (!backwards.empty()) {
				times += backwards.top().times;
				height = backwards.top().height;
				if (height + field[backwards.top().end] * (d - d2 + times) < b) {
					times -= backwards.top().times;
					increaseTimes(d - d2 + times);///////////
					break;
				}

				quantity = backwards.top().end - backwards.top().start + 1;
				start = backwards.top().start;

				pushBack = false;
				if (backwards.empty() ? height + field[0] * (d - d2 + times) < b : height + field[backwards.top().start] * (d - d2 + times) < b) {
					long long position = binsearch2(backwards.top().start, backwards.top().end);
					result += (backwards.top().end - position + 1) * (height - b) + (suffixSum[position] - suffixSum[backwards.top().end + 1]) * (d - d2 + times);
					border = position;
					pushBack = true;
					break;
				} else {
					result += quantity * (height - b) + (suffixSum[backwards.top().start] - suffixSum[backwards.top().end + 1]) * (d - d2 + times);
					border = backwards.top().start;
				}

				backwards.pop();
			}
			if (pushBack) {
				backwards.pop();
				backwards.push(Struct(border - 1, height, d - d2 + times, start));
			}
		}

		d2 = d;
		b2 = b;

		cout << result << endl;
	}



	return 0;
}