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
#include<iostream>
#include<cstdio>
#include<string>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
#include<utility>
using namespace std;

#define FOR(I,A,B) for(int I=(A);I<=(B);I++)
#define REP(I,N) for(int I=0;I<(N);I++)
#define ALL(X) (X).begin(),(X).end()
#define PB push_back
#define MP make_pair
#define f first
#define s second

typedef vector<int> VI;
typedef pair<int,int> PII;
typedef long long ll;
typedef vector<string> VS;

const int MAX = 500005;

long long prefix_sums[MAX];
vector<int> growth_rate;
vector< tuple<int, long long, long long> > levels;

long long find_height(int idx, long long d) {
	auto b = lower_bound(levels.begin(), levels.end(), make_tuple(idx, -1, -1));
	if(b == levels.end() || get<0>(*b) > idx) --b;

//cout << get<0>(*b) << " " << get<1>(*b) << " " << get<2>(*b) << endl;
//cout << growth_rate[get<0>(*b)] << endl;
	int _d = get<1>(*b);
	int _b = get<2>(*b);

	return _b + (d - _d) * growth_rate[idx];

}

long long get_interval_sum(int a, int b) {
	return prefix_sums[b] - (a == 0 ? 0 : prefix_sums[a - 1]);
}

int find_bound(int d, int b) {
	int l,r;

	l = 0;
	r = growth_rate.size();

	while(l < r) {
		int s = (l + r - 1) / 2;
		
		if(find_height(s, d) >= b) {
			r = s;
		} else {
			l = s + 1;
		}
	}

	if(find_height(l, d) < b) {
		return -1;
	} else {
		return l;
	}
}

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

	int n,m;
	cin >> n >> m;

	REP(i,n) {
		int r;
		cin >> r;
		growth_rate.PB(r);
	}

	sort(ALL(growth_rate));

	prefix_sums[0] = growth_rate[0];
	FOR(i,1,n-1) {
		prefix_sums[i] = prefix_sums[i - 1] + growth_rate[i];
	}

	levels.PB(make_tuple(0,0,0));
	
	REP(j,m) {
		long long d, b;
		cin >> d >> b;

		long long result = 0;
		int idx = find_bound(d, b);
		int prev_idx = n;

//		cout << "idx: " << idx << endl;

		while(!levels.empty() && idx <= get<0>(levels.back())) {
			auto a = levels.back();

			result += (d - get<1>(a))*get_interval_sum(get<0>(a), prev_idx - 1);
	//		cout << get<0>(a) << " " << prev_idx - 1 << " " << get_interval_sum(get<0>(a), prev_idx-1) << endl;
	//		cout << result << endl;
			result += (get<2>(a) - b) * (prev_idx - get<0>(a));
//cout << "result: " << result << endl;
			levels.pop_back();
			prev_idx = get<0>(a);
		}

		if(!levels.empty()) {
			auto a = levels.back();

			result += (d - get<1>(a))*get_interval_sum(idx, prev_idx - 1);
	//		cout << "r1: " << result << endl;
	//		cout << get<2>(a) - b << endl;
			result += (get<2>(a) - b) * (prev_idx - idx);
		}

		levels.push_back(make_tuple(idx, d, b));

/*		cout << "levels\n";
		REP(j,levels.size()) {
			cout << get<0>(levels[j]) << " " << get<1>(levels[j]) << " " << get<2>(levels[j]) << endl;
		}
*/
/*		cout << "debug\n";
		cout << "a" << find_height(0,2) << endl;
		cout << "b"<<find_height(1,2) << endl;
		cout << "c"<<find_height(2,2) << endl;
		cout << "d"<<find_height(3,2) << endl;
*/
		//long long sum = get_interval_sum(idx);
		//long long diff_d = d - prev_d;

/*cout << idx << endl;
cout << sum << endl;
		cout << diff_d*sum - (n - idx)*b << '\n';*/
		cout << result << '\n';
	}

	return 0;
}