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
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
using namespace std;
#define inf 1000000005
typedef long long ll;
typedef double db;
void gn(int &x){
    int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c=='-')sg=-1,x=0;else x=c-'0';
    while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';
    x*=sg;
}
void gn(ll &x){
    int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');
    if(c=='-')sg=-1,x=0;else x=c-'0';
    while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';
    x*=sg;
}
const int mo=1000000007;
int qp(int a,ll b){int ans=1;do{if(b&1)ans=1ll*ans*a%mo;a=1ll*a*a%mo;}while(b>>=1);return ans;}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int n,m;
int a[555555];
ll s[555555];
struct node{
	ll t,b;
	int r;
}stk[555555];int top;
int main()
{
	scanf("%d%d",&n,&m);
	for (int i=1;i<=n;i++)gn(a[i]);
	sort(a+1,a+1+n);
	s[0]=0;
	for (int i=1;i<=n;i++)s[i]=s[i-1]+a[i];
	stk[0].r=0;
	top=1;
	stk[top].t=stk[top].b=0;
	stk[top].r=n;
	while(m--){
		ll d,b;
		gn(d);
		gn(b);
		ll su=0;
		while(top){
			ll hi,lo;
			hi=a[stk[top].r]*(d-stk[top].t)+stk[top].b;
			lo=a[stk[top-1].r+1]*(d-stk[top].t)+stk[top].b;
			if(b>=hi)break;
			if(b<=lo){
				su+=(s[stk[top].r]-s[stk[top-1].r])*(d-stk[top].t)+(stk[top].b-b)*(stk[top].r-stk[top-1].r);
				top--;
				continue;
			}else{
				int lef=stk[top-1].r+1,rig=stk[top].r,mid;
				while(lef<=rig){
					mid=lef+rig>>1;
					ll h=a[mid]*(d-stk[top].t)+stk[top].b;
					if(h<=b)lef=mid+1;
					else rig=mid-1;
				}
				su+=(s[stk[top].r]-s[rig])*(d-stk[top].t)+(stk[top].b-b)*(stk[top].r-rig);
				stk[top].r=rig;
				break;
			}
		}
		printf("%lld\n",su);
		if(stk[top].r==n)continue;
		++top;
		stk[top].t=d;stk[top].b=b;stk[top].r=n;
	}
	return 0;
}