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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <cstdio>
#include <set>
#include <algorithm>
#include <vector>
#include <cstdlib>
#include <map>

using namespace std;

typedef long long ll;

static
map<ll, int> toasts;

struct gap {
	long long etacol;
	long long endpos;
	int cnt;
};

struct cmp_eta {
	bool operator()(gap a, gap b){
		if(a.etacol != b.etacol) return a.etacol < b.etacol;
		if(a.endpos != b.endpos) return a.endpos < b.endpos;
		return a.cnt != b.cnt;
	}
};

struct cmp_end {
	bool operator()(gap a, gap b){
		if(a.endpos != b.endpos) return a.endpos < b.endpos;
		if(a.etacol != b.etacol) return a.etacol < b.etacol;
		return a.cnt != b.cnt;
	}
};

static
set<gap, cmp_eta> gaps_eta;
static
set<gap, cmp_end> gaps_end;
static
int n, m;
static
long long add_per_round = 0;

static
long long per(gap g){
	long long c = g.cnt;
	return c*(c-1)/2;
}

static
void rem(gap g){
	add_per_round -= per(g);
	gaps_eta.erase(g);
	gaps_end.erase(g);
}

static
void add(gap g){
	add_per_round += per(g);
	gaps_eta.insert(g);
	gaps_end.insert(g);
}

static
void print_gap(gap g){
	printf("end = %lld, eta = %lld, cnt = %d\n", g.endpos, g.etacol, g.cnt);
}

static
void print_gaps(){
	for(auto g: gaps_end){
		print_gap(g);
	}
}

#define INF 1000000000000000009LL
static
ll ceildiv(ll a, ll b){
	return (a+b-1)/b;
}

static
long long overdue = 0;
static
ll now;
// Time, overdue, per_round
static
vector<pair<ll, pair<ll, ll>>> prep;

static
gap combine(gap left, gap right) {
	ll shiftby = left.cnt * now + left.endpos - right.endpos;
	overdue += right.cnt * shiftby;
	gap res;
	res.endpos = left.endpos;
	res.cnt = right.cnt + left.cnt;
	auto it = gaps_end.find(right);
	it++;
	if (it == gaps_end.end()) {
		res.etacol = INF;
	}
	else {
		res.etacol = ceildiv(it->endpos - res.endpos, res.cnt);
	}
	return res;
}

#define dbg 0
static
long long lastmerge = 0;

static
void merge(){
	gap first = *gaps_eta.begin();
	now = first.etacol;
	overdue += (now - lastmerge) * add_per_round;
	if (dbg) {
		printf("\nAt time %lld, gap:\n  ", now);
		print_gap(first);
		printf("  collides\n");
	}
	while (1) {
		auto it = gaps_end.find(first);
		it++;
		auto other = *it;
		auto combined = combine(first, other);
		rem(first);
		rem(other);
		add(combined);
		if (dbg) {
			printf("  causing merge of:\n    ");
			print_gap(first);
			printf("    ");
			print_gap(other);
			printf("  to:\n    ");
			print_gap(combined);
		}
		if (combined.etacol > now) break;
		first = combined;
	}
	lastmerge = now;
}

static
void init(){
	long long last = 0;
	int prev = 1;
	for(auto t: toasts){
		gap g = {ceildiv(t.first - last, prev), last, prev};
		add(g);
		last = t.first;
		prev = t.second;
	}
	gap g = {INF, last, prev};
	add(g);
	while (gaps_eta.size() > 1) {
		if(dbg) {
			printf("\n\nCurrent state (overdue = %lld, per = %lld):\n", overdue, add_per_round);
			print_gaps();
		}
		prep.push_back({now, {overdue, add_per_round}});
		merge();
	}
	prep.push_back({now, {overdue, add_per_round}});
	prep.push_back({INF, {INF, INF}});
	if (dbg) {
		printf("\n\nCurrent state (overdue = %lld, per = %lld):\n", overdue, add_per_round);
		print_gaps();
		printf("\n\n");
		for (auto p: prep) {
			printf("t=%lld, cost=%lld, +%lld per\n", p.first, p.second.first, p.second.second);
		}
	}
}

static
ll answer(int bake){
	if(dbg)
		printf("bake %d\n", bake);
	pair<ll, pair<ll, ll>> pp(bake, {INF, INF});
	auto it = lower_bound(prep.begin(), prep.end(), pp);
	it--;
	ll start = it->first;
	ll cost = it->second.first;
	ll add = it->second.second;

	ll total = cost + add * (bake - start);
	if(dbg)
		printf("%lld %lld %lld\n", start, cost, add);
	//printf("t=%d, total=%lld\n", bake, total);
	return total;

	/*
	long long last = 0;
	long long penalty = 0;
	for(auto t: toasts){
		for(int j=0; j<t.second; j++){
			long long time = t.first;
			if(last + bake <= time) {
				last = time;
			}
			else {
				penalty += last + bake-time;
				last += bake;
			}
		}
	}
	//printf("t=%d, total=%lld\n", bake, penalty);
	return penalty;
	*/
}

int main(){
	scanf("%d %d", &n, &m);
	for(int i=0; i<n; i++){
		ll t;
		scanf("%lld", &t);
		toasts[t]++;
	}
	init();
	vector<pair<int, int>> tests;
	for(int i=0; i<m; i++){
		int x;
		scanf("%d", &x);
		tests.push_back({x, i});
	}
	sort(tests.begin(), tests.end());
	vector<ll> results(m);
	for(auto t: tests){
		results[t.second] = answer(t.first);
	}
	if(1)
		for(auto r: results){
			printf("%lld\n", r);
		}
}