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
//Szymon Wozniak
#include <cstdio>
#include <algorithm>
#include <vector>


using namespace std;

typedef long long ll;

ll n;
ll m;
ll aa[500006];
ll d[500006];
ll b[500006];



int main()
{
	ll res = 0;
	scanf("%lld", &n);
	scanf("%lld", &m);
	
	ll x;
	vector<long long> a;
	
	for(ll i = 0; i < n; i++)
	{
		scanf("%lld", &x);
		aa[i] = x;
		a.push_back(x);	
	}

	for(ll i = 0; i < m; i++)
	{
		scanf("%lld %lld", &d[i], &b[i]);
	}
	
	
	sort(a.begin(),a.end());
	sort(aa,aa+n);
	
	
	
	vector<long long>::iterator it;	
	//ll last = 0;
	for(ll i = 0; i < m; i++)
	{
		
		ll pos = (lower_bound(a.begin(),a.end(),b[i]) - a.begin() );
		
		//ut << "pos = " << pos << endl;
		for(ll j = pos; j < n; j++)
		{
			res += a[j] - b[i];
			a[j] = b[i]+aa[j];
		}
		
		//res += pom[pos]*(d[i]-last); 
		//res += (a[pos]-b[i])*(d[i]-last)*( (n-1)-pos+1 );
		printf("%lld\n", res);
		res = 0;
		//last = d[i];
			
	}
	
	//printf("%lld\n", res);
	
	return 0;
}