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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
//#ifdef DEBUG
//#define _GLIBCXX_DEBUG
//#endif
#pragma GCC optimize("O3")
#include<bits/stdc++.h>

using namespace std;

#ifdef DEBUG

#include "lib/debug.h"

#else
#define debug(...) 228
#endif

#define pb push_back

typedef long long ll;
typedef long double ld;
int n, m;
const int M = 64;
const int maxN = 5e4 + 5;
int a[maxN];
const int S = 408 - 64;
int id[M + 2][M + 2];
pair<int, int> val[S];
int st[M + 1];
int en[M + 1];
//cnt of each type
typedef array<int, S> arr;
arr hlp[M + 2];
arr shift;
arr ident;
vector<int> poses[M + 1];
void prep() {
    int cnt = 0;
    for (int i = 1; i <= M; i++) {
        st[i] = cnt;
        for (int j = 0; j <= M; j += i) {
            val[cnt] = {i, j};
            poses[j].emplace_back(cnt);
            id[i][j / i] = cnt++;
        }
        en[i] = cnt;
    }
    assert(cnt == S);
    for (int x = 0; x < cnt; x++) {
        if (val[x].first + val[x].second <= M) shift[x] = x + 1;
        else shift[x] = st[val[x].first];
    }
    for (int x = 0; x <= M + 1; x++) {
        for (int y = 0; y < cnt; y++) {
            if (val[y].second <= x) hlp[x][y] = y;
            else hlp[x][y] = st[val[y].first];
        }
    }
    for (int x = 0; x < S; x++) ident[x] = x;
}

//for permutations
int b[maxN];

const int maxP = (1 << 17) + 2;
int t[maxP][S];
arr lazy[maxP];
typedef unsigned long long ull;
ull msk[maxP];
bool need[maxP];
int buf[S];
const ull one = 1;
void apply(int v, arr& f) {
//    ++pushh;
    memset(buf, 0, sizeof buf);
    for (int i = 1; i <= M; i++) {
        if (msk[v] & (one << (i - 1))) {
            for (int j = st[i]; j < en[i]; j++) {
                lazy[v][j] = f[lazy[v][j]];
                buf[f[j]] += t[v][j];
            }
        }
    }
    need[v] = true;
    memcpy(t[v], buf, sizeof(int) * S);
}

void merge(int v, int tl, int tr) {
    int tm = (tl + tr) / 2;
    int nv = v + 2 * (tr - tm);
    for (int i = 0; i < S; i++) {
        t[v][i] = t[v + 1][i] + t[nv][i];
    }
}

void push(int v, int tl, int tr) {
    if (need[v]) {
        int tm = (tl + tr) / 2;
        int nv = v + 2 * (tr - tm);
        apply(v + 1, lazy[v]);
        apply(nv, lazy[v]);
        need[v] = false;
        lazy[v] = ident;
        return;
    }
    //OPT!!!
}

void apply(int v, int tl, int tr, int cut, int &cnt_cut) {
    //>cut -> 0
    if (cnt_cut == 0) {
        for (int i = 1; i <= cut; i++) {
            if (cut % i == 0) {
                int good_pos = st[i] + cut / i;
                if (t[v][good_pos]) {
                    for (int j = st[i]; j < en[i]; j++) {
                        if (lazy[v][j] == good_pos) lazy[v][j] = st[i];
                    }
                    t[v][st[i]] += t[v][good_pos];
                    t[v][good_pos] = 0;
                }
            }
        }
        return;
    }
    int sum = 0;
    for (const int& d : poses[cut]) {
        sum += t[v][d];
    }
    //>{cut} -> 0  //cut ?
    if (sum <= cnt_cut) {
        cnt_cut -= sum;
        return;
    }
    int tm = (tl + tr) / 2;
    push(v, tl, tr);
    apply(v + 1, tm + 1, tr, cut, cnt_cut);
    apply(v + 2 * (tr - tm), tl, tm, cut, cnt_cut);
    merge(v, tl, tr);
}

void set_node(int v, int tl, int tr, int pos, int vval) {
    t[v][vval] += 1;
    msk[v] |= (one << (val[vval].first - 1));
    if (tl == tr) return;
//    if (tl == tr) {
//        t[v][val] = 1;
//        return;
//    }
    push(v, tl, tr);
    int tm = (tl + tr) / 2;
    if (pos <= tm) set_node(v + 2 * (tr - tm), tl, tm, pos, vval);
    else set_node(v + 1, tm + 1, tr, pos, vval);
//    t[v][val] += 1;
//    merge(v, tl, tr);
}

void print_tree(int v, int tl, int tr) {
    if (tl == tr) {
        for (int j = 0; j < S; j++) {
            if (t[v][j]) {
                assert(t[v][j] == 1);
                cout << val[j].second << " ";
            }
        }
        return;
    }
    push(v, tl, tr);
    int tm = (tl + tr) / 2;
    print_tree(v + 2 * (tr - tm), tl, tm);
    print_tree(v + 1, tm + 1, tr);
}

int nb[maxN];

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    auto start = std::chrono::high_resolution_clock::now();
#ifdef DEBUG
    freopen("input.txt", "r", stdin);
#endif
    prep();
    cin >> n >> m;
    mt19937 rnd(228);
//    n = 50'000;
//    m = rnd() % 5000'0000 + 1;
    for (int i = 1; i < maxP; i++) {
        lazy[i] = ident;
        need[i] = false;
    }
    if (n == 1) {
        cout << m << '\n';
        return 0;
    }
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
//        a[i] = 1;
//        if (rnd() % 6 == 0) a[i] = rnd() % 64 + 1;
//        a[i] = i % 64 + 1;
    }
    b[n] = m;
    nb[n] = m;
    b[n - 1] = m;
    int L = n;
    int R = n;
    vector<int> help_freq(M + 1);
    if (m + a[n] <= M) help_freq[m + a[n]]++;
    for (int i = n - 1; i >= 1; i--) {
        while (L <= R) {
            if (b[R] != -1 && (b[R] <= M && b[R] % a[R] == 0)) {
                int p = (b[R] / a[R]);
                int nxt = (b[R] + a[R]);
                if (nxt <= M) help_freq[nxt] -= 1;
                set_node(1, 1, n, R, id[a[R]][p]);
                R--;
            }
            else {
                break;
            }
        }
        auto freq = help_freq;
        int cnt_good = 0;
        for (int v : freq) cnt_good += v;
        for (int p = 0; p < S; p++) {
            if (t[1][p] == 0) continue;
            int x = val[p].first;
            int y = val[p].second;
            y += x;
            if (y <= M) {
                freq[y] += t[1][p];
                cnt_good += t[1][p];
            }
        }
        int need = (n - i) / 2;
        assert(cnt_good >= (n - i) / 2);
        int our_sum = 0;
        int cut = -1;
        int cnt_cut = 0;
        for (int x = 0; x <= M; x++) {
            int d = min(freq[x], need);
            if (need == d) {
                cut = x;
                cnt_cut = d;
                need -= d;
                our_sum += d * x;
                break;
            }
            need -= d;
            our_sum += d * x;
        }
        assert(need == 0 && cut != -1);
        if (our_sum <= m) {
            b[i] = m - our_sum;
            apply(1, shift);
            apply(1, hlp[cut]);
            if (cut != 0) {
                apply(1, 1, n, cut, cnt_cut);
            }
            fill(help_freq.begin(), help_freq.end(), 0);
            L--;
            for (int j = R; j >= L; j--) {
                if (j != i) {
                    if (b[j] == -1) {
                        b[j] = 0;
                    } else {
                        b[j] += a[j];
                    }
                    if (b[j] > cut) b[j] = 0;
                    else if (b[j] == cut) {
                        if (cnt_cut > 0) cnt_cut--;
                        else b[j] = 0;
                    }
                }
                int nxt = (b[j] == -1 ? 0 : (b[j] + a[j]));
                if (nxt <= M) help_freq[nxt] += 1;
            }
        } else {
            b[i] = -1;
            L--;
            help_freq[0] += 1;
        }
    }
    for (int j = L; j <= R; j++) {
        cout << b[j] << " ";
    }
    print_tree(1, 1, n);
    auto end = std::chrono::high_resolution_clock::now();
    std::cerr << "Execution Time: "
              << std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count()
              << " ms" << std::endl;
    return 0;
}