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
#include <bits/stdc++.h>
using namespace std;
#define e1 first
#define e2 second
#define pb push_back
#define mp make_pair
#define boost ios_base::sync_with_stdio(false)
#define eb emplace_back
#define OUT(x) {cout << x; exit(0); }
typedef long long ll;
typedef unsigned long long ull;
typedef pair <int, int> PII;
typedef pair <ll, ll> PLL;
typedef unsigned int ui;
const int mod = 1e9+7;
const int inf = 1e9+9;
const ll MOD = 1e9+696969;
const ll INF = 1e18+3;
#define maxn 500100
const int R = (1 << 19);
ll t[maxn], roz;
PLL cut[maxn];
ll pref[2 * R + 5], cnt[2 * R + 5];
int n, N, m;
int dr[maxn + 100];
ll W[2 * R + 5], w[2 * R + 5], visit[2 * R + 5];
bool akt[2 * R + 5];
int X, Y;
ll WART = 0, RET = 0;

inline void napraw(int x, int y, ll WA) {
	X = x; Y = y; WART = WA;
}
inline void phaj(int gdzie) 
{
	akt[gdzie] = 0;
	visit[gdzie << 1] = visit[(gdzie << 1) + 1] = visit[gdzie];
	w[gdzie << 1] = w[(gdzie << 1) + 1] = w[gdzie];
	akt[gdzie << 1] = akt[(gdzie << 1) + 1] = 1;
}

inline void insert_rek(int gdzie, int pocz, int kon, ll czas)
{
	if (akt[gdzie]) W[gdzie] = w[gdzie] * (kon - pocz + 1);
	if (X > kon || pocz > Y) return;
	
	if (X <= pocz && Y >= kon)
	{
		RET += W[gdzie] + cnt[gdzie] * (czas - visit[gdzie]);
		akt[gdzie] = 1;
		w[gdzie] = WART;
		visit[gdzie] = czas;
		W[gdzie] = w[gdzie] * (kon - pocz + 1);
		return;
	}
	
	if (akt[gdzie]) phaj(gdzie);
	int sr = (pocz + kon) >> 1;
	insert_rek(gdzie << 1, pocz, sr, czas);
	insert_rek((gdzie << 1) + 1, sr + 1, kon, czas);
	W[gdzie] = W[gdzie << 1] + W[(gdzie << 1) + 1];
	W[gdzie] += cnt[gdzie << 1] * (czas - visit[gdzie << 1]);
	W[gdzie] += cnt[(gdzie << 1) + 1] * (czas - visit[(gdzie << 1) + 1]);
	visit[gdzie] = czas;
}

inline ll readll(){
    ll x = 0; char c;
    for(; ((c<48 || c>57) && c != '-'); c = getchar_unlocked());
    for(; c>47 && c<58 ; c = getchar_unlocked()) x = (x<<1) + (x<<3) + c - '0';
	return x;
}
inline int readint(){
	int x = 0; char c;
	for(; ((c<48 || c>57) && c != '-'); c = getchar_unlocked());
    for(; c>47 && c<58 ; c = getchar_unlocked()) x = (x<<1) + (x<<3) + c - '0';
    return x;
}

inline void radd(int p, int x) {
	for (; p > 0; p -= p & (-p)) dr[p] = x;	
}
inline int rget(int p) {
	int s = 0;
	for (; p < maxn; p += p & (-p)) s = max(dr[p], s);
	return s;
}
inline bool check(int poz)
{
	if (poz > n) return true;
	int ost = rget(N - poz + 1);
	ll wyn = cut[ost].e2 + t[poz] * (cut[roz].e1 - cut[ost].e1);
	if (wyn > cut[roz].e2) return true;
	return false;
}
inline int binsearch(int x, int y) {
	while (x < y) {
		int sr = (x + y) >> 1;
		if (check(sr)) y = sr; //jest skoszone
		else x = ++sr;
	}
	return x;
}
inline void insert(int x, int help) {
	radd(N - x + 1, help);
}
int main()
{
	n = readint(); m = readint();
	N = n + 5;
	for (int i=1; i<=n; ++i) t[i] = readll();
	cut[0] = mp(0, 0); pref[0] = 0;

	sort(t + 1, t + n + 1);
	for (int i=1; i<=n; ++i) pref[i] = pref[i-1] + t[i];
	for (int i=1; i<=n; ++i) cnt[i + R] = t[i];
	for (int i=R -1; i>0; --i) cnt[i] = cnt[i << 1] + cnt[(i << 1) + 1];
	for (int i = n + 1; i<=(R + 5); ++i) pref[i] = pref[i-1];

	for (int z=1; z<=m; ++z)
	{
		cut[z].e1 = readll();  cut[z].e2 = readll();
		roz = z;
		int poz = binsearch(1, n + 1); //szukamy pierwszej pozycji, ktora zostala scieta
		if (poz > n)
		{
			puts("0");
			continue;
		}
		insert(poz, z);
		napraw(poz, n, cut[z].e2);
		RET = 0;
		insert_rek(1, 0, R - 1, cut[z].e1);
		ll sum = RET;	
		ll dl = max(0, n - poz + 1);
		ll ILE = dl * cut[z].e2;
		printf("%lld\n", sum - ILE);
	}
}