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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
typedef long long LL;

int n, m;
int *unitVec;
LL *sumVec;

struct Cut{
	int from;
	LL day;
	LL height;
	
	Cut() {}
	Cut(int fr, LL d, LL h) : from(fr), day(d), height(h) {}
};

vector<Cut> cuts;

LL getSum(int from, int to){ // both inclusive
	return sumVec[to] - (from > 0 ? sumVec[from-1] : 0LL);
}

LL makeCut(LL day, LL h){
	int pos = -1, cutPos = 0;
	LL weight = 0LL;
	int total = (int)cuts.size();
	
	int imin = 0, imax = total-1;
	
	while(imin <= imax){
		int imid = (imin + imax) / 2;
		LL midVal = cuts[imid].height + unitVec[cuts[imid].from] * (day - cuts[imid].day);
			
		if(h >= midVal)
			imin = imid + 1;
		else
			imax = imid - 1;
	}
	
	cutPos = imax;
	
	if(cutPos < 0){
		cutPos = 0;
		pos = 0;
	}else{
		int a = cuts[cutPos].from;
		int b = (cutPos < total-1 ? cuts[cutPos+1].from-1 : n-1);
		LL cutHeight = cuts[cutPos].height;
		LL days = day - cuts[cutPos].day;
	
		while(a <= b){
			int mid = (a + b) / 2;
			LL midVal = cutHeight + unitVec[mid] * days;
			
			if(h >= midVal)
				a = mid + 1;
			else
				b = mid - 1;
		}
		
		if(a >= n)
			pos = -1;
		else if(cutPos < total-1 && a >= cuts[cutPos+1].from){
			cutPos++;
			pos = a;
		}else
			pos = a;
	}
	
	/*cutPos = 0;
	for(int i=0; i<n; i++){
		if(cutPos < total-1 && i >= cuts[cutPos+1].from) cutPos++;
		
		if(cuts[cutPos].height + unitVec[i] * (day - cuts[cutPos].day) > h){
			pos = i;
			break;
		}
	}*/
	
	if(pos < 0) return 0LL;
	
	for(int i = total-1; i >= cutPos && i >= 0; i--){
		int from = max(cuts[i].from, pos);
		int to = (i < total-1 ? cuts[i+1].from-1 : n-1);
		
		int dist = to - from + 1;
		weight += cuts[i].height * dist + getSum(from, to) * (day - cuts[i].day) - h * dist;
	}
	
	if(cutPos < total-1)
		cuts.erase(cuts.begin() + (cutPos + 1), cuts.end());
	
	if(pos == cuts[cutPos].from)
		cuts.erase(cuts.begin() + cutPos);

	cuts.push_back(Cut(pos, day, h));
	
	return weight;
}

int main(){
	int a;
	LL d, b;
	
	scanf("%d %d", &n, &m);
	
	unitVec = new int[n];
	sumVec = new LL[n];
	
	for(int i=0; i<n; i++){
		scanf("%d", &a);
		unitVec[i] = a;
	}
	
	sort(unitVec, unitVec + n);
	
	LL sum = 0;
	
	for(int i=0; i<n; i++){
		sum += unitVec[i];
		sumVec[i] = sum;
	}
	
	cuts.push_back(Cut(0, 0, 0)); // zero cut
	
	for(int i=0; i<m; i++){
		scanf("%lld %lld", &d, &b);
		LL result = makeCut(d, b);
		printf("%lld\n", result);
		
		//for(int k=0; k<(int)cuts.size(); k++)
			//printf("-- from:%d day:%lld h:%lld\n", cuts[k].from, cuts[k].day, cuts[k].height);
	}
	
	delete [] unitVec;
	delete [] sumVec;
	
	return 0;
}