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
#include <bits/stdc++.h>
using namespace std;

int main(){
	ios_base::sync_with_stdio(false), cin.tie(nullptr);
	int N, M;
	cin >> N >> M;
	vector<int> A(N);
	for(int i = 0; i < N; i++) cin >> A[i];
	// NEVER pay a pirate more than 64.
	// you can always pay pirates who weren't paid previous day for d[i]
	// which = (n-1) // 2 + 1 (yourself)
	// N * 64^2 = 2e8
	const int G = 65;
	// greed = a, cost = b
	const int B = 20;
	const int S = 50000 / B; // i / 1000

	vector<vector<int> > divisors(G+1);
	for(int g = 1; g <= G; g++){
		for(int d = 1; d <= G; d++){
			if(g % d == 0) divisors[g].push_back(d);
		}
	}

	vector<int> par(N, -1);

	int cnts[G][G+1];
	// head, tail, cnt
	vector<tuple<int,int,int> > locs[G][G+1];
	for(int g = 0; g < G; g++) for(int x = 0; x <= G; x++) {
		cnts[g][x] = 0;
		locs[g][x].assign(B, {-1, -1, 0});
	}

	vector<vector<int> > extras(G+1); // when elected or dead, go here

	auto combine = [&](int d, int src_x, int dst_x, int bucket){
		auto& [shead, stail, scnt] = locs[d][src_x][bucket];
		if(scnt == 0) return;
		auto& [dhead, dtail, dcnt] = locs[d][dst_x][bucket];
		cnts[d][src_x] -= (int)scnt;
		cnts[d][dst_x] += (int)scnt;
		if(dcnt == 0){
			dhead = shead;
			dtail = stail;			
		} else {
			par[dhead] = stail;
			dhead = shead;
		}
		shead = stail = -1;
		dcnt += scnt;
		scnt = 0;
	};

	auto attach = [&](tuple<int, int, int>& nv, int v) -> void {
		if(get<2>(nv)){
			par[v] = get<1>(nv);
			get<1>(nv) = v;
			get<2>(nv)++;
		} else {
			par[v] = -1;
			nv = {v, v, 1};
		}
	};
	int profit;
	vector<int> bucket_extras_tmp(B);
	vector<int> active_tmp(S);

	vector<int> ans(N, -1);
	for(int i = N-1; i >= 0; i--){
		int need_votes = (N-i+1)/2 - 1;
		int votes = 0;
		int pay = 0;
		int amt_pay = 0;
		for(; pay <= G; pay++){
			int count_pay = 0;
			for(int d : divisors[pay]){
				count_pay += cnts[d][pay/d];
			}
			count_pay += (int)extras[pay].size();
			votes += count_pay;
			amt_pay += count_pay * pay;
			if(votes >= need_votes){
				break;
			}
		}
		int extra_votes = votes - need_votes;
		amt_pay -= extra_votes * pay;
		if(amt_pay <= M){
			bucket_extras_tmp.assign(B, 0);
			for(int v : extras[pay]){
				bucket_extras_tmp[v / S]++;
			}
			// find thresh
			int thresh = 0;
			for(int bucket = 0; bucket < B; bucket++){
				if(extra_votes == 0) break;
				int count_pay_bucket = 0;
				count_pay_bucket += bucket_extras_tmp[bucket];
				for(int g : divisors[pay]){
					count_pay_bucket += get<2>(locs[g][pay/g][bucket]);
				}
				if(count_pay_bucket <= extra_votes){
					thresh += S;
					extra_votes -= count_pay_bucket;
					for(int g : divisors[pay]){
						combine(g, pay/g, 0, bucket);
					}
				} else {
					active_tmp.assign(S, 0);
					for(int v : extras[pay]){
						if(v / S == bucket){
							active_tmp[v - bucket * S] = 1;
						}
					}
					for(int g : divisors[pay]){
						int v = get<1>(locs[g][pay/g][bucket]);
						while(v != -1){
							active_tmp[v - bucket * S] = 1;
							v = par[v];
						}
					}
					for(int c = 0; c < S; c++){
						thresh++;
						if(active_tmp[c]){
							extra_votes -= 1;
							if(extra_votes == 0) break;
						}
					}
					assert(extra_votes == 0);
					for(int g : divisors[pay]){
						int v = get<1>(locs[g][pay/g][bucket]);
						tuple<int, int, int> nv = {-1, -1, 0};
						while(v != -1){
							int pv = par[v];
							if(v < thresh){
								cnts[g][pay/g] -= 1;
								cnts[g][0] += 1;
								attach(locs[g][0][bucket], v);
							} else {
								attach(nv, v);
							}
							v = pv;
						}
						locs[g][pay/g][bucket] = nv;
					}
					break;
				}
			}
			// sort extras
			vector<vector<int> > nextras(G+1);
			for(int p = 0; p <= G; p++){
				for(int v : extras[p]){
					if(p > pay || (p == pay && v < thresh)){
						cnts[A[v]][0] += 1;
						attach(locs[A[v]][0][v / S], v);
					} else {
						nextras[min(G, p + A[v])].push_back(v);
						ans[v] = p;
					}
				}
			}
			extras = nextras;
			for(int g = 1; g < G; g++){
				int x = pay/g + 1;
				for(int y = x; y <= (G-1) / g + 1; y++){
					if(cnts[g][y]){
						for(int bucket = 0; bucket < B; bucket++){
							combine(g, y, 0, bucket);
						}
					}
				}
				rotate(locs[g], locs[g] + x, locs[g] + x+1);
				rotate(cnts[g], cnts[g] + x, cnts[g] + x+1);
			}

			profit = M - amt_pay;
			extras[min(profit + A[i], G)].push_back(i);
			ans[i] = profit;
		} else {
			// can't pay
			extras[0].push_back(i);
			ans[i] = -1;
		}
	}
	for(int g = 1; g < G; g++){
		for(int y = 0; y <= (G-1) / g + 1; y++){
			for(int bucket = 0; bucket < B; bucket++){
				int a = get<1>(locs[g][y][bucket]);
				while(a != -1){
					ans[a] = g * (y-1);
					a = par[a];
				}
			}
		}
	}
	for(int i = 0; i < N; i++){
		cout << ans[i] << " \n"[i+1 == N];
	}
}