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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//Piotr Golda

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

#define LLD unsigned long long int
#define UD unsigned long long int

class Mow;


UD N, M;

LLD T[500000];
LLD Sum[500000];

vector<Mow> MowStack;

class Mow
{
public:
	UD m_day; 
	LLD m_height;
	UD m_field;

	Mow(UD p_day, LLD p_height, UD p_field)
		: m_day(p_day), m_height(p_height), m_field(p_field)
	{ }
};



void scan()
{
	cin >> N >> M;
	for (UD i = 0L; i < N; ++i)
	{
		cin >> T[i];
	}
}

void initialProcess()
{
	sort(T, T + N);
	Sum[0] = T[0];
	for (UD i = 1; i < N; ++i)
	{
		Sum[i] = Sum[i - 1] + T[i];
	}

	MowStack.push_back(Mow(0L, 0L, 0L));
}


struct CmpClass
{
public:
	LLD m_prev_height;
	LLD m_days;
	LLD m_height;
	bool operator()(LLD p_lhs, LLD p_rhs)
	{
		if (p_rhs == 0L)
		{
			return m_prev_height + m_days*p_lhs < m_height;
		}
		if (p_lhs == 0L)
		{
			return m_prev_height + m_days*p_rhs > m_height;
		}
		return p_lhs < p_rhs;
	}

};

//starts with p_left, ends in p_right-1
LLD partialHarvest(UD p_left, UD p_right, LLD p_height, LLD p_oldHeight, LLD p_days)
{
	if (p_left == p_right)
		return 0;
	LLD harv = Sum[p_right-1];
	if (p_left > 0L)
		harv -= Sum[p_left-1];
	harv *= p_days;
	harv += (p_oldHeight-p_height)*(LLD)(p_right-p_left);
	return harv;
}


UD findMowStart(Mow& p_mow, Mow& p_prevMow, LLD p_finishField)
{
	CmpClass cmp;
	cmp.m_days = (LLD)(p_mow.m_day - p_prevMow.m_day);
	cmp.m_prev_height = p_prevMow.m_height;
	cmp.m_height = p_mow.m_height;
	UD start = upper_bound(T + p_prevMow.m_field, T + p_finishField, 0L, cmp) - T;
	p_mow.m_field = start;
	return start;
}

LLD actuallMowHeight(Mow p_mow, LLD p_day)
{
	return p_mow.m_height + (p_day - p_mow.m_day)*T[p_mow.m_field];
}


LLD computeYields(UD p_day, LLD p_height)
{
	Mow newMow(p_day, p_height, 0L);
	LLD tmp = 0;
	if (MowStack.size() > 0)
		tmp = MowStack[MowStack.size() - 1].m_height;
	if (newMow.m_height >= tmp + (newMow.m_day - MowStack[MowStack.size() - 1].m_day)*T[N - 1])
		return 0;

	LLD ans = 0L;
	UD prevStart = N;
	while (MowStack.size() > 0 && actuallMowHeight(MowStack.back(), (LLD)(p_day)) > p_height)
	{
		Mow& m = MowStack.back();
		ans += partialHarvest(m.m_field, prevStart, p_height, m.m_height, p_day - m.m_day);
		prevStart = m.m_field;
		MowStack.pop_back();
	}
	if (MowStack.size() > 0)
	{
		Mow& m = MowStack.back();
		UD start = findMowStart(newMow, m, prevStart);
		ans += partialHarvest(start, prevStart, p_height, m.m_height, p_day - m.m_day);
	}

	MowStack.push_back(newMow);
	return ans;
}


void mainLoop()
{
	LLD ans;
	LLD h; //height
	LLD k;
	for (UD i = 0L; i < M; ++i)
	{
		//scanf("%lld", &k);
		//scanf("%lld", &h);
		cin >> k >> h;

		ans = computeYields(k, h);
		printf("%lld\n", ans);
	}
}


int main()
{
	ios_base::sync_with_stdio(0);
	scan();
	initialProcess();
	mainLoop();

	return 0;
}