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
#include<iostream>
#include<set>
#include<algorithm>
#include<climits>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<ll,pll> plll;//[place;[height;day]]
set<plll> knownCuts;
//ll lastCut[1<<20];
ll growSpeed[1<<20];
const int firstElement=1<<19;

/*ll findLastCutRecursive(int p,int q)
{
	ll maks=0;
	if(p>q)
	{
		return 0;
	}
	if(p==q)
	{
		return lastCut[p];
	}
	if(p%2==1)
	{
		maks=max(maks,lastCut[p]);
		p++;
	}
	if(q%2==0)
	{
		maks=max(maks,lastCut[q]);
		q--;
	}
	return max(maks,findLastCutRecursive(p/2,q/2));
	
}

ll findLastCut(int p,int q)
{
	return findLastCutRecursive(p+firstElement,q+firstElement);
}
*/
ll findGrowSpeedRecursive(int p,int q)
{
	ll sum=0;
	if(p>q)
	{
		return 0;
	}
	if(p==q)
	{
		return growSpeed[p];
	}
	if(p%2==1)
	{
		sum+=growSpeed[p];
		p++;
	}
	if(q%2==0)
	{
		sum+=growSpeed[q];
		q--;
	}
	return sum+findGrowSpeedRecursive(p/2,q/2);
	
}

ll findGrowSpeed(int p,int q)
{
	return findGrowSpeedRecursive(p+firstElement,q+firstElement);
}
bool isHigher(int position,ll height,ll day)
{
	auto ee=knownCuts.lower_bound(plll(position,pll(LLONG_MAX,LLONG_MAX)));
	ee--;
	plll firstLower=*ee;
	ll myHeight=(day-firstLower.second.second)*growSpeed[firstElement+position]+firstLower.second.first;
	return myHeight>height;
//(day-findLastCut(0,position))*growSpeed[firstElement+position]+;
}

int main()
{
	int n,q;
	cin>>n>>q;
	for(int i=0;i<n;i++)
	{
		cin>>growSpeed[firstElement+i];
	}
	sort(growSpeed+firstElement,growSpeed+firstElement+n);
	for(int i=firstElement-1;i>0;i--)
	{
		growSpeed[i]=growSpeed[i*2]+growSpeed[i*2+1];
	}
	//ll lastDay=0,onTheFields=0;
	knownCuts.insert(plll(0,pll(0,0)));
	for(int k=0;k<q;k++)
	{
		ll day,height;
		cin>>day>>height;
		int start=0,end=n;
		while(start!=end)
		{
			int s=(start+end)/2;
			if(isHigher(s,height,day))
				end=s;
			else
				start=s+1;
		}
		auto ee=knownCuts.lower_bound(plll(start,pll(LLONG_MAX,LLONG_MAX)));
		ee--;
		plll firstLower=*ee;
		firstLower.first=start;
		plll nextCut;
		ll result=0;
		while((nextCut=*(knownCuts.lower_bound(plll(firstLower.first,pll(LLONG_MAX,LLONG_MAX)))))!=(*(knownCuts.end())))
		{
			//cerr<<'x'<<endl;
			result+=(day-firstLower.second.second)*findGrowSpeed(firstLower.first,nextCut.first-1)-(nextCut.first-firstLower.first)*(height-firstLower.second.first);
			firstLower=nextCut;
		}
		//cerr<<"growSpeed:"<<findGrowSpeed(firstLower.first,n)<<endl;
		result+=(day-firstLower.second.second)*findGrowSpeed(firstLower.first,n-1)-(n-firstLower.first)*(height-firstLower.second.first);
		cout<<result<<endl;
		ee=knownCuts.lower_bound(plll(start,pll(LLONG_MAX,LLONG_MAX)));
		ee--;
		if((*ee).first!=start)
			ee++;
		auto lastOfSet=knownCuts.end();	
		knownCuts.erase(ee,lastOfSet);
		knownCuts.insert(plll(start,pll(height,day)));
	}
}