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
#include <bits/stdc++.h>

#define DB 0

using namespace std;

typedef long long int ll;

const int MAXN = 1 << 19;

int n, m;
ll a[MAXN], sa[MAXN], totalLossSum[MAXN];

struct Cut {
	ll time, height;
	Cut () { 
		time = height = 0LL;
	}
	Cut(ll _time, ll _height) { 
		time = _time, height = _height;
		
	}
} C[MAXN];

struct Interval {
	int lastCut;
	int beginning, end;
	ll totalLoss() {
		return (sa[end] - sa[beginning-1]) * C[lastCut].time - C[lastCut].height * length();
	}
	
	ll length() {
		return end - beginning + 1;
	}
	
	Interval() {
		lastCut = 0;
		beginning = 1;
		end = n;
	}
	
	Interval(int _lastCut, int _beginning, int _end) {
		lastCut = _lastCut;
		beginning = _beginning;
		end = _end;
	}
	
	void print() {
		printf("%d -- %d: %d\n", beginning, end, lastCut);
	}
};

vector<Interval> I;

ll height(int pos, int lastCut, ll currentTime) {
	return C[lastCut].height + (currentTime - C[lastCut].time) * a[pos];
}

int findIntervalEnd(int lastCut, int currentCut, int beginning, int end) {
	int mid;
	while (end - beginning > 3) {
		mid = (beginning + end)/2;
		if (height(mid, lastCut, C[currentCut].time) >= C[currentCut].height)
			beginning = mid;
		else
			end = mid;
	}
	for (int i = end; i >= beginning; i--) {
		if (height(i, lastCut, C[currentCut].time) >= C[currentCut].height)
			return i;
	}
	return beginning - 1;
}

void insertInterval(int cutNo) {
	if (DB) {
		for (auto it = I.begin(); it != I.end(); it++)
			it->print();
		printf("\n");
	}
	
	totalLossSum[cutNo] = totalLossSum[cutNo-1];
	
	while (!I.empty() && height(I.back().end, I.back().lastCut, C[cutNo].time) >= C[cutNo].height) {
		totalLossSum[cutNo] -= I.back().totalLoss();
		I.pop_back();
	}
	if (I.empty()) {
		I.push_back(Interval(cutNo, 1, n));
		totalLossSum[cutNo] += I.back().totalLoss();
	}
	else {
		int intervalEnd = findIntervalEnd(I.back().lastCut, cutNo, I.back().beginning, I.back().end);
		
		if (intervalEnd == 0) return;
		
		totalLossSum[cutNo] -= I.back().totalLoss();
		I.back().beginning = intervalEnd + 1;
		totalLossSum[cutNo] += I.back().totalLoss();
		I.push_back(Interval(cutNo, 1, intervalEnd));
		totalLossSum[cutNo] += I.back().totalLoss();
	}
	
	if (DB) {
		for (auto it = I.begin(); it != I.end(); it++)
			it->print();
		printf("\n");
	}
}

int main() {
	
	scanf("%d%d", &n, &m);
	for (int i = 1; i <= n; i++) scanf("%lld", &a[i]);
	
	sort (a+1, a+n+1, greater<ll>());
	for (int i = 1; i <= n; i++) sa[i] = sa[i-1] + a[i];
	
	I.push_back(Interval(0, 1, n));
	
	for (int i = 1; i <= m; i++) {
		scanf("%lld%lld", &C[i].time, &C[i].height);
		
		insertInterval(i);
		
		ll kochamjulie = totalLossSum[i] - totalLossSum[i-1];
		
		printf("%lld\n", kochamjulie);
	}
	
	return 0;
}