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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("O3")

// hloya template v26

// ░░░░░░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░░░░░
// ░░░░░░█░░▄▀▀▀▀▀▀▀▀▀▀▀▀▀▄░░█░░░░░
// ░░░░░░█░█░▀░░░░░▀░░▀░░░░█░█░░░░░
// ░░░░░░█░█░░░░░░░░▄▀▀▄░▀░█░█▄▀▀▄░
// █▀▀█▄░█░█░░▀░░░░░█░░░▀▄▄█▄▀░░░█░
// ▀▄▄░▀██░█▄░▀░░░▄▄▀░░░░░░░░░░░░▀▄
// ░░▀█▄▄█░█░░░░▄░░█░░░▄█░░░▄░▄█░░█
// ░░░░░▀█░▀▄▀░░░░░█░██░▄░░▄░░▄░███
// ░░░░░▄█▄░░▀▀▀▀▀▀▀▀▄░░▀▀▀▀▀▀▀░▄▀░
// ░░░░█░░▄█▀█▀▀█▀▀▀▀▀▀█▀▀█▀█▀▀█░░░
// ░░░░▀▀▀▀░░▀▀▀░░░░░░░░▀▀▀░░▀▀░░░░

#include <bits/stdc++.h>
using namespace std;

bool dbg = 0;

clock_t start_time = clock();
#define current_time                                                           \
  fixed << setprecision(6) << (ld)(clock() - start_time) / CLOCKS_PER_SEC

#define f first
#define s second
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define eb emplace_back
#define all(v) (v).begin(), (v).end()
#define sz(v) ((int)(v).size())
#define sqr(x) ((x) * (x))

#define ull unsigned long long
#define ll long long
#define ld long double
#define pii pair<int, int>
#define umap unordered_map<int, int>

#define files1 freopen("input.txt", "r", stdin)
#define files2 freopen("output.txt", "w", stdout)
#define files                                                                  \
  files1;                                                                      \
  files2
#define fast_io                                                                \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0)

// #define endl '\n'
#define ln(i, n) " \n"[(i) == (n)-1]

void bad(string mes = "NO") {
  cout << mes;
  exit(0);
}
void bad(int mes) {
  cout << mes;
  exit(0);
}

template <typename T> string bin(T x, int st = 2) {
  string ans = "";
  while (x > 0) {
    ans += char('0' + x % st);
    x /= st;
  }
  reverse(ans.begin(), ans.end());
  return ans.empty() ? "0" : ans;
}

mt19937_64 mt_rand(228 // chrono::system_clock::now().time_since_epoch().count()
);

template <typename T1, typename T2> inline bool upmax(T1 &a, T2 b) {
  return (a < b ? (a = b, true) : false);
}
template <typename T1, typename T2> inline bool upmin(T1 &a, T2 b) {
  return (b < a ? (a = b, true) : false);
}

// inline int popcount(int x){
//     int count = 0;
//     __asm__ volatile("POPCNT %1, %0;":"=r"(count):"r"(x):);
//     return count;
// }

template <typename T> T input() {
  T ans = 0, m = 1;
  char c = ' ';

  while (!((c >= '0' && c <= '9') || c == '-')) {
    c = getchar();
  }

  if (c == '-')
    m = -1, c = getchar();
  while (c >= '0' && c <= '9') {
    ans = ans * 10 + (c - '0'), c = getchar();
  }
  return ans * m;
}

template <typename T> T gcd(T a, T b) {
  while (b) {
    a %= b;
    swap(a, b);
  }
  return a;
}

template <typename T> void read(T &a) { a = input<T>(); }
template <typename T> void read(T &a, T &b) { read(a), read(b); }
template <typename T> void read(T &a, T &b, T &c) { read(a, b), read(c); }
template <typename T> void read(T &a, T &b, T &c, T &d) {
  read(a, b), read(c, d);
}

const int inf = 1e9 + 20;
const short short_inf = 3e4 + 20;
const long double eps = 1e-12;
const int maxn = (int)2e5 + 3, base = 998244353;
const ll llinf = 2e18 + 5;
const int mod = 1e9 + 7;

vector<int> solve(vector<int> a, int m) {
  if (a.size() == 1) {
    return {m};
  }
  if (a.size() == 2) {
    return {0, m};
  }
  auto as = a;
  as.pop_back();

  auto d = solve(as, m);
  int k = (a.size() + 1) / 2 - 1;

  vector<int> min_b(d.size());
  for (int i = 0; i < d.size(); ++i) {
    if (d[i] == -1) {
      min_b[i] = 0;
    } else {
      min_b[i] = d[i] + a[i];
    }
  }

  vector<pii> sorted_pairs;
  for (int i = 0; i < d.size(); ++i) {
    sorted_pairs.pb({min_b[i], i});
  }
  sort(all(sorted_pairs));

  ll min_sum = 0;

  vector<int> res(d.size());
  for (int i = 0; i < k; ++i) {
    min_sum += sorted_pairs[i].f;
    res[sorted_pairs[i].s] = sorted_pairs[i].f;
  }

  if (min_sum <= m) {
    res.push_back(m - min_sum);
    return res;
  } else {
    d.push_back(-1);
    return d;
  }
}

vector<int> solveFast(vector<int> a, int m) {
  vector<int> d = {m};

  int od = 0, prin = 0;
  for (int i = 1; i < a.size(); ++i) {
    int curn = i + 1;
    int k = (curn + 1) / 2 - 1;

    // simple sort, then change to dih
    vector<pii> sorted_pairs;

    set<int> counts;

    int non_neg = i - 1;
    while (non_neg >= 0 && d[non_neg] < 0) {
      non_neg--;
    }
    // assert(non_neg >= 0);

    for (int j = 0; j < i; ++j) {
      assert(j >= non_neg || a[i] <= 128);
      // if (j < non_neg && i > 10) {
      //   if (!(d[j] == 0 || d[j] % a[j] == 0)) {
      //     cout << "WRONG" << endl;
      //     cout << d[j] << ' ' << a[j] << ' ' << j << ' ' << i << endl;
      //     cout << non_neg << endl;

      //     for (auto x : d) {
      //       cout << x << ' ';
      //     }
      //     cout << endl;
      //     exit(228);
      //   }
      // }
      sorted_pairs.pb({d[j] + a[j], j});
      counts.insert(d[j] + a[j]);
    }
    // around 80 different values
    // we need to find k first
    // then set d[i] = -a[i] OR

    nth_element(sorted_pairs.begin(), sorted_pairs.begin() + k,
                sorted_pairs.end());
    // cout << "counts = " << counts.size() << endl;
    // cout << "max = " << sorted_pairs[k - 1].f << endl;
    // cout << "max value = " << *counts.rbegin() << endl;
    // if (counts.size() > 2) {
    //   cout << "second max = " << *next(counts.rbegin()) << endl;
    //   cout << "third max = " << *next(next(counts.rbegin())) << endl;
      assert(*next(counts.rbegin()) <= 128);
    // }

    ll min_sum = 0;
    for (int j = 0; j < k; ++j) {
      min_sum += sorted_pairs[j].f;
    }
    if (min_sum <= m) {
      ll left = m - min_sum;
      // занулить всех
      int cnt_neg = 0;
      for (int j = 0; j < i; ++j) {
        if (d[j] < 0) {
          ++cnt_neg;
        }
      }
      for (int j = 0; j < i; ++j) {
        d[j] = 0;
      }
      for (int j = 0; j < k; ++j) {
        d[sorted_pairs[j].s] = sorted_pairs[j].f;
      }
      d.push_back(left);
    } else {
      d.push_back(-a[i]);
    }
  }
  for (auto &x : d) {
    if (x < 0) {
      x = -1;
    }
  }
  // cout << "prin = " << prin << ", od = " << od << endl;
  return d;
}

vector<int> lists[65][129]; // a[i], c[i]

vector<int> divs[129];

int take_list_prefix[65];
int take_from_big = 0;


vector<int> solveVeryFast(vector<int> a, int m) {
  if (a.size() == 1) {
    return {m};
  }
  if (a.size() == 2) {
    return {0, m};
  }
  // dbg = 1;
  if (dbg) {
    cout << "Debug = " << dbg << endl;
  }
  int ccdivs = 0;
  for (int i = 1; i <= 64; ++i) {
    for (int j = 1; j <= 128; ++j) {
      lists[i][j].clear();
    }
  }
  for (int i = 1; i <= 128; ++i) {
    divs[i].clear();
    for (int j = 1; j <= i; ++j) {
      if (i % j == 0) {
        divs[i].pb(j);
        ccdivs++;
      }
    }
  }

  vector<pair<int, int>> big;
  big.push_back({m + a[1], 1});
  lists[a[0]][1].push_back(0);

  vector<int> odz;

  // a = 1, 1, 1, 1, 1
  // 0,1
  //

  for (int i = 2; i < a.size(); ++i) {
    sort(all(big)); // should be <= 64 elements

    int curn = i + 1;
    int k = (curn + 1) / 2 - 1;

    int selected = 0;
    int big_ptr = 0;
    bool finish_selection = false;

    ll min_sum = 0;

    for (int val = 0; val <= 128; ++val) {
      int cands_cnt = 0;
      if (val == 0) {
        cands_cnt += odz.size();
      }

      int big_ptr_prev = big_ptr;
      while (big_ptr < big.size() && big[big_ptr].f == val) {
        ++cands_cnt;
        ++big_ptr;
      }

      // a[i] * c == val
      for (auto c : divs[val]) {
        int a_val = val / c;
        if (a_val <= 64) {
          cands_cnt += lists[a_val][c].size();
        }
      }

      selected += cands_cnt;

      if (selected >= k) {
        finish_selection = true;

        int to_get_with_val = k - (selected - cands_cnt);
        // how to understand where to split???
        min_sum += val * to_get_with_val;

        for (int a_val = 1; a_val <= 64; ++a_val) {
          take_list_prefix[a_val] = 0;
        }
        take_from_big = 0;

        if (dbg) {
          cout << "[i, val, min_sum, to_get_with_val] = " << i << ' ' << val
               << ' ' << min_sum << ' ' << to_get_with_val << endl;
          cout << "odz = ";
          for (auto x : odz) {
            cout << x << ' ';
          }
          cout << endl;
          cout << "big = ";
          for (auto [val, id_big] : big) {
            cout << "[" << val << ", " << id_big << "] ";
          }
          cout << endl;
        }
        assert(to_get_with_val > 0);

        if (dbg) {
          for (int a_val = 1; a_val <= 64; ++a_val) {
            for (int c = 1; c <= 128; ++c) {
              if (lists[a_val][c].size() > 0) {
                cout << "lists[" << a_val << "][" << c << "] = ";
                for (auto x : lists[a_val][c]) {
                  cout << x << ' ';
                }
                cout << endl;
              }
            }
          }
        }

        if (val == 0) {
          to_get_with_val -= odz.size();
        //   assert(to_get_with_val == 0); // take only ODZ
        } else {
          auto count = [&](int prefix) -> int {
            int res = 0;
            take_from_big =
                upper_bound(big.begin() + big_ptr_prev, big.begin() + big_ptr,
                            make_pair(val, prefix)) -
                big.begin() - big_ptr_prev;
            res += take_from_big;

            for (auto c : divs[val]) {
              auto a_val = val / c;
              if (a_val <= 64) {
                take_list_prefix[a_val] =
                    upper_bound(lists[a_val][c].begin(), lists[a_val][c].end(),
                                prefix) -
                    lists[a_val][c].begin();
                res += take_list_prefix[a_val];
              }
            }
            return res;
          };

          int l = 0, r = i;
          while (l < r) {
            int mid = (l + r) / 2;
            if (count(mid) < to_get_with_val) {
              l = mid + 1;
            } else {
              r = mid;
            }
          }
          int cur_res = count(l);

          if (dbg) {
            cout << "l = " << l << endl;
            cout << "count(l) = " << count(l) << endl;
            // cout << "take_list_prefix = ";
            // for (int a_val = 1; a_val <= 64; ++a_val) {
            //   cout << take_list_prefix[a_val] << ' ';
            // }
            // cout << endl;
          }
        }

        if (min_sum <= m) {
          ll left = m - min_sum;
          big_ptr = big_ptr_prev + take_from_big;
          vector<pii> big_to_kill =
              vector<pii>(big.begin() + big_ptr, big.end());
          big.resize(big_ptr);
          for (auto &[val, id_big] : big) {
            val += a[id_big];
          }

          // move all lists
          for (int a_val = 1; a_val <= 64; ++a_val) {
            // zero where a_val * c > val (move to 1)
            // split where a_val * c == val (right part to merging, left part
            // leave) move +1
            vector<int> merged_to_zero;
            // for (int c = 1; c * a_val <= 128; ++c) {
            for (int c = 128 / a_val; c >= 1; --c) {
              if (c * a_val == val) {
                // SLOW
                // assert(take_list_prefix[a_val] <= lists[a_val][c].size());
                move(lists[a_val][c].begin() + take_list_prefix[a_val],
                     lists[a_val][c].end(), back_inserter(merged_to_zero));
                lists[a_val][c].resize(take_list_prefix[a_val]);
              } else if (c * a_val > val) {
                move(lists[a_val][c].begin(), lists[a_val][c].end(),
                     back_inserter(merged_to_zero));
                lists[a_val][c].clear();
              }
            }
            if (dbg) {
              if (merged_to_zero.size() > 0) {
                cout << "merged_to_zero = ";
                for (auto x : merged_to_zero) {
                  cout << x << ' ';
                }
                cout << endl;
              }
            }
            // 
            if (!is_sorted(all(merged_to_zero))) {
              sort(all(merged_to_zero));
            }
            // assert(is_sorted(all(merged_to_zero)));

            int max_idx = 128 / a_val;
            // assert(lists[a_val][max_idx + 1].empty());
            for (int c = max_idx; c >= 1; --c) {
              swap(lists[a_val][c], lists[a_val][c + 1]);
            }
            swap(lists[a_val][1], merged_to_zero);
          }

          sort(all(big_to_kill),
               [&](const pii &a, const pii &b) { return a.s < b.s; });
          
          
          for (auto [val, id_big] : big_to_kill) {
            int iter = upper_bound(lists[a[id_big]][1].begin(), lists[a[id_big]][1].end(), id_big) - lists[a[id_big]][1].begin();
            lists[a[id_big]][1].insert(lists[a[id_big]][1].begin() + iter, id_big);
          }
          for (int id_odz : odz) {
            lists[a[id_odz]][1].push_back(id_odz);
          }
          odz.clear();
          big.push_back({left + a[i], i});
        } else {
          // odz
          odz.push_back(i);
        }
        break;
      } else {
        min_sum += val * cands_cnt;
      }
    }
    // assert(finish_selection);

    if (dbg) {
      vector<int> res(a.size());
      for (auto x : odz) {
        res[x] = -1;
      }
      for (auto [val, id_big] : big) {
        res[id_big] = val - a[id_big];
      }
      for (int a_val = 1; a_val <= 64; ++a_val) {
        for (int c = 1; c * a_val <= 128; ++c) {
          for (auto id : lists[a_val][c]) {
            res[id] = (c - 1) * a_val;
          }
        }
      }

      cout << "Tmp res: \n";
      for (int j = 0; j <= i; ++j) {
        cout << res[j] << ' ';
      }
      cout << endl;
      if (i == 7) {
        exit(0);
      }
    }
  }

  vector<int> res(a.size());
  for (auto x : odz) {
    res[x] = -1;
  }
  for (auto [val, id_big] : big) {
    res[id_big] = val - a[id_big];
  }
  for (int a_val = 1; a_val <= 64; ++a_val) {
    for (int c = 1; c * a_val <= 128; ++c) {
      for (auto id : lists[a_val][c]) {
        res[id] = (c - 1) * a_val;
      }
    }
  }
  return res;
}

int main() {
  // files1;
  fast_io;

  int n, m;
  cin >> n >> m;
  // n = 50000;
  // m = 4238336;
  vector<int> a(n);
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    // a[i] = rand() % 64 + 1;
    // a[i] = 6;
  }
  reverse(all(a));
  auto res = solveVeryFast(a, m);
  reverse(all(res));
  for (auto x : res) {
    cout << x << " ";
  }
  return 0;
}