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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<cstdio>
#include<algorithm>
#include<vector>
#include<set>
#include<map>
#include<queue>
#include<cmath>
#include<iostream>
#include<string>
using namespace std;
#define F first
#define S second
#define MP make_pair
#define PB push_back
#define LL long long
#define PII pair<int, int>
#define PLL pair<LL, LL>
const int M=1024*512;

struct node
{
	LL sum, maxi, maxind, time, cut;
};

int n, m, T[M];
LL P[M];
node D[2*M];

inline void lejzi(int v, int vp, int vk)
{
	LL a=D[v].cut, b=D[v].time;
	D[2*v].cut=a;
	D[2*v].time=b;
	D[2*v+1].cut=a;
	D[2*v+1].time=b;
	D[2*v].sum=a*(LL)((vk-vp+1)/2);
	D[2*v+1].sum=a*(LL)((vk-vp+1)/2);
	D[2*v].maxi=a;
	D[2*v+1].maxi=a;
	D[v].cut=-1;
}

inline void make(int v, LL t, int vp, int vk)
{
	D[v].time=t;
	D[v].sum=D[2*v].sum+D[2*v+1].sum+(P[(vp+vk)/2]-P[vp-1])*(t-D[2*v].time)+(P[vk]-P[(vp+vk)/2])*(t-D[2*v+1].time);
	if((vp+vk)/2+1<=n)
		D[v].maxi=D[2*v+1].maxi+(t-D[2*v+1].time)*(LL)T[D[2*v+1].maxind];
	else
		D[v].maxi=D[2*v].maxi+(t-D[2*v].time)*(LL)T[D[2*v].maxind];
}

void addmin(int v, int vp, int vk, int p, int k, LL w, LL t)
{
	if(vp>k || vk<p)
		return;
	if(vp>=p && vk<=k)
	{
		D[v].time=t;
		D[v].cut=w;
		D[v].sum=w*(LL)(vk-vp+1);
		D[v].maxi=w;
		return;
	}
	int mid=(vp+vk)/2;
	if(D[v].cut!=-1)
		lejzi(v, vp, vk);
	addmin(2*v, vp, mid, p, k, w, t);
	addmin(2*v+1, mid+1, vk, p, k, w, t);
	make(v, t, vp, vk);
}

LL sum(int v, int vp, int vk, int p, int k, LL t)
{
	if(vp>k || vk<p)
		return 0;
	if(vp>=p && vk<=k)
		return D[v].sum+(t-D[v].time)*(P[vk]-P[vp-1]);
	int mid=(vp+vk)/2;
	if(D[v].cut!=-1)
		lejzi(v, vp, vk);
	LL r=sum(2*v, vp, mid, p, k, t)+sum(2*v+1, mid+1, vk, p, k, t);
	make(v, t, vp, vk);
	return r;
}

int getpos(int v, int vp, int vk, LL x, LL t)
{
	//printf(" %d\n", v);
	if(D[v].maxi+(t-D[v].time)*(LL)T[D[v].maxind]<x)
		return -1;
	if(v>=M)
		return v-M;
	if(D[v].cut!=-1)
		lejzi(v, vp, vk);
	int mid=(vp+vk)/2;
	int p=getpos(2*v, vp, mid, x, t);
	if(p==-1)
		p=getpos(2*v+1, mid+1, vk, x, t);
	make(v, t, vp, vk);
	return p;
}

int main()
{
	//ios_base::sync_with_stdio(0);
	scanf("%d%d", &n, &m);
	for(int i=1; i<=n; i++)
		scanf("%d", T+i);
	sort(T+1, T+n+1);
	for(int i=1; i<=n; i++)
		P[i]=T[i]+P[i-1];
	for(int i=n+1; i<M; i++)
		P[i]=P[i-1];
	for(int i=0; i<M; i++)
	{
		D[i+M].maxind=i;
		D[i+M].cut=-1;
	}
	for(int i=M-1; i>0; i--)
	{
		D[i].cut=-1;
		if(T[D[2*i].maxind]<T[D[2*i+1].maxind])
			D[i].maxind=D[2*i+1].maxind;
		else
			D[i].maxind=D[2*i].maxind;
	}
	for(int i=1; i<=m; i++)
	{
		LL a, b;
		scanf("%lld%lld", &a, &b);
		int x=getpos(1, 0, M-1, b, a);
		//printf("%d %lld %lld\n", x, D[4+M].maxi, D[5+M].maxi);
		if(x==0)
			x++;
		if(x==-1)
		{
			printf("0\n");
			continue;
		}
		printf("%lld\n", sum(1, 0, M-1, x, n, a)-(LL)(n-x+1)*b);
		addmin(1, 0, M-1, x, n, b, a);
	}
	return 0;
}