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
#include <iostream>
#include <set>
#include <vector>

using namespace std;

struct ciecie{
	int pg;
	int h;
	int r;
	int v;
};

struct punkt{
	int r;
	int h;
};

struct prwn{
	bool operator()(const ciecie a, const ciecie b){
		if(a.pg < b.pg) return true;
		if(a.r > b.r) return true;
		return false;
		
	}
};

int n, m, x, mx = -1;
int sort_t[1000003];
int t[500003];
unsigned int p[500003];
//ciecie c[500003];
ciecie c, h1;
set<ciecie, prwn> s;
set<ciecie, prwn>::iterator it;
punkt tree[1048576];
int sum;
int M, h2;
punkt tmpx;
// obsluga drzewka i bin-searcha

punkt tree_query(int v){
	v += M;
	punkt newest;
	newest.r = -1;
	while(v > 0){
		if(tree[v].r > newest.r) newest = tree[v];
		v/=2;
	}
	return newest;
}

int bin_search(int h, int cr){
	int start = 0;
	int end = n-1;
	int center = -1;
	while(start < end){
		center = (start+end+1)/2;
		tmpx = tree_query(center);
		if(tmpx.r == -1) h2 = cr*t[center];
		else{
			h2 = tmpx.h+(cr-tmpx.r)*t[center];
		}
		if( h2 <= h) start = center;
		else end = center-1;
		
	}
	cout << start << " no kurwa to " << tree_query(start).h + (cr-tree_query(start).r)*t[center] << endl;
	if(tree_query(start).h + (cr-tree_query(start).r)*t[center] <= h) start++;
	return start;
}


void tree_insert(ciecie c1){
	int l = c1.pg+M;
	int r = n-1+M;
	while(l != r){
		tree[l].h = c1.h;
		tree[l].r = c1.r;
		tree[r].h = c1.h;
		tree[r].r = c1.r;
		if(l%2 == 1){
			l = l+1;
			tree[l].h = c1.h;
			tree[l].r = c1.r;
		}
		if(r%2 == 1){
			r = r-1;
			tree[r].h = c1.h;
			tree[r].r = c1.r;
		}
		r/=2;l/=2;
	}
	
	
}



int first_bigger(int tmp){
	int i = 2;
	while(i < tmp){
		i*=2;
	}
	return i;
}


int main(){
	cin >> n >> m;
	M = first_bigger(n);
	for(int i = 0; i < n; ++i){
		cin >> x;
		if(x > mx) mx = x;
		sort_t[x]++;
	}
	x = 0;
	for(int i = 0; i <= mx; ++i){
		for(int j = 0; j < sort_t[i]; ++j, ++x){
			t[x] = i;
		}
	}
	p[0] = t[0];
	for(int i = 1; i < n; ++i){
		p[i] = p[i-1]+t[i];
	}
	sum = 0;
	
	for(int i = 0; i < m; ++i){
		
		cin >> c.r >> c.h;
		
		c.pg = bin_search(c.h, c.r);
		cout << "pg: " << c.pg << endl;
		tree_insert(c);
		
		s.insert(c);
		it = s.find(c);
		it++;
		sum = c.r*(p[n-1]-p[c.pg])-(n-1-c.pg)*c.h;
		while(it != s.end()){
			h1 = *it;
			sum -= h1.v;
			it++;
		}
		it = s.find(c);
		it++;
		s.erase(it, s.end());
		s.erase(c);
		cout << "size: " << s.size() << endl;
		c.v = sum;
		s.insert(c);
		if(c.v < 0){
			s.erase(c);
			c.v = 0;
			
		}
		cout << c.v << endl;
	}
	return 0;
}