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
#include <iostream>
#include <algorithm>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int t[1000000];
int w[1000000];
int main(int argc, char** argv) 
{
	int n,q;
	cin >> n >> q;
	
	for (int i=0; i <n; i++)
	{
		cin >> t[i];
		w[i]=t[i];
	}
	sort (t,t +n);
	sort(w, w+n);
int a,b,pom=0;
for (int j=0; j <q; j++)
{
	long long wynik=0;
	cin >> a >> b;
	
	for (int i=0; i<n; i++)
	{
		if(j!=0)
		t[i]+=w[i]*(a-pom);
		if(t[i] >b)
		{	
			wynik +=t[i]-b;
			t[i]=b;
			
		}
	//	cout << t[i] << "    ";
	}
//	cout << endl;
	
	cout << wynik << endl;
	pom=a;
	wynik =0;
}
	return 0;
}