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
#include <bits/stdc++.h>
using namespace std;

#define fru(j,n) for(int j=0; j<(n); ++j)
#define tr(it,v) for(typeof((v).begin()) it=(v).begin(); it!=(v).end(); ++it)
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define ALL(G) (G).begin(),(G).end()

typedef long long ll;
typedef double D;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef vector<int> vi;

const int inft = 1000000009;
const int MAXN = 1000006,T=512*1024;

int A[MAXN];
ll suf[MAXN];
pll drzh[2*T],drzsum[2*T];
int n,m;

void updateh(int nr,int L,int R,int a,int b,pll value){
	if(b<=L || a>=R)return;
	if(a<=L && b>=R){drzh[nr]=value;return;}
	updateh(2*nr,L,(L+R)/2,a,b,value);
	updateh(2*nr+1,(L+R)/2,R,a,b,value);
}
ll getsum(int nr,int L,int R,int a,int b,ll day,pll best){
	if(b<=L || a>=R)return 0;
	else if(a<=L && b>=R){
		pll w=drzsum[nr];
		if(best.x>w.x)w=pll(best.x,1LL*(R-L)*best.y);
		return w.y+(day-w.x)*(suf[L]-suf[R]);
	}
	return getsum(2*nr,L,(L+R)/2,a,b,day,max(best,drzh[nr]))+
		getsum(2*nr+1,(L+R)/2,R,a,b,day,max(best,drzh[nr]));
	//	printf("%d %lld %lld\n",nr,drzsum[nr].x,drzsum[nr].y);
	//	printf("getsum nr %d, L %d, R %d, a%d, b %d, day %lld -> ret %lld\n",nr,L,R,a,b,day,ret);
}
ll updatesum(int nr,int L,int R,int a,int b,ll h,ll day, pll best){
	ll ret=0;
	if(b<=L || a>=R){
		pll w=drzsum[nr];
		if(best.x>w.x)w=pll(best.x,1LL*(R-L)*best.y);
		//	printf("upd %d %d odcz %lld %lld\n",L,R,w.x,w.y);

		return w.y+(day-w.x)*(suf[L]-suf[R]);
	}
	if(a<=L && b>=R){drzsum[nr]=pll(day,h*(R-L));if(0)printf("%d %d %lld %lld\n",L,R,day,h*(R-L));return drzsum[nr].y;}
	ret+=updatesum(2*nr,L,(L+R)/2,a,b,h,day,max(best,drzh[nr]));
	ret+=updatesum(2*nr+1,(L+R)/2,R,a,b,h,day,max(best,drzh[nr]));
	drzsum[nr]=pll(day,ret);
	//	printf("updatesum nr %d, L %d, R %d, a%d, b %d, h %lldday %lld -> ret %lld\n",nr,L,R,a,b,h,day,ret);
	return ret;
}

void solve() {
	scanf("%d%d",&n,&m);
	fru(i,n)scanf("%d",A+i);
	sort(A,A+n);
	suf[n]=0;
	for(int i=n-1;i>=0;i--)suf[i]=A[i]+suf[i+1];
	fru(i,2*T)drzh[i]=pll(0,0);
	fru(i,2*T)drzsum[i]=pll(0,0);
	ll day,h;
	int it_bound=0;
	vector<pair<int,pll> > bound(m+5);
	bound[it_bound++]=make_pair(0,pll(0,0));
	fru(i,m){
		scanf("%lld%lld",&day,&h);
		int pocz=0,kon=it_bound,nr,nr_seg;
		if(bound[0].y.x+(day-bound[0].y.y)*A[bound[0].x]>h)nr=0;
		else{
			while(pocz<kon-1){
				int med=(pocz+kon)/2;
				if(bound[med].y.x+(day-bound[med].y.y)*A[bound[med].x]>h)
					kon=med;
				else pocz=med;
			}
			nr_seg=pocz;//mozliwe, ze poczatek nr_seg+1;
			pocz=bound[nr_seg].x;
			kon=n;
			if(nr_seg+1<it_bound)kon=bound[nr_seg+1].x;
			pll w=bound[nr_seg].y;
			while(pocz<kon-1){
				int med=(pocz+kon)/2;
				if(w.x+(day-w.y)*A[med]>h)
					kon=med;
				else pocz=med;
			}
			nr=kon;
		}
		if(nr==n){puts("0");continue;}
		//dorzuc do bound
		while(it_bound>0 && bound[it_bound-1].x>=nr){it_bound--;}
		bound[it_bound++]=make_pair(nr,pll(h,day));
	//	printf("bounds -> %d, %lld %lld\n",nr,h,day);
	//	printf("nr %d\n",nr);
		ll ans=getsum(1,0,n,nr,n,day,pll(0,0))-h*(n-nr);
		printf("%lld\n",ans);
		updatesum(1,0,n,nr,n,h,day,pll(0,0));
		updateh(1,0,n,nr,n,pll(day,h));
	}
}

int main() {
	//	freopen("input.in", "r", stdin);
	//	freopen("output.out", "w", stdout);
	int t=1;
	//	scanf("%d",&t);
	fru(i,t) solve();
	return 0;
}