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
import java.util.Arrays;
import java.util.Scanner;

public class sia {

	public static void main(String[] args) {
		int n,m,lday=0,cday=0,cut,gdays,cutindx=-1;
		long w=0;
		Scanner clintp = new Scanner(System.in);
		n= clintp.nextInt();
		m= clintp.nextInt();
		
		int grow []=new int [n] ;
		long hight[]=new long[n] ;
		
		for(int tmp=0;tmp<n;tmp++){
			grow[tmp]=clintp.nextInt();		
			hight[tmp]=0;
		}
		
		Arrays.sort(grow);
		for(;m>0;m--){
			cday= clintp.nextInt();
			gdays=cday-lday;
			lday=cday;
			cut= clintp.nextInt();
			
			for(int tmp=0;tmp<n;tmp++){
				hight[tmp]+=gdays*grow[tmp];
				if(cutindx==-1&&hight[tmp]>cut){
					cutindx=tmp;
				}
			}
			if(cutindx==-1)System.out.println(0);
			else{
				for(int tmp=cutindx;tmp<n;tmp++){
						w+=hight[tmp]-cut;
						hight[tmp]=cut;
					}
				System.out.println(w);
			}
			cutindx=-1;
			w=0;
		}
	clintp.close();			
	}

}