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

#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) begin(x), end(x)
#define sz(x) int((x).size())
using ll = long long;
using pii = pair<int, int>;
using vi = vector<int>;

#ifdef LOCAL
auto operator<<(auto& o, auto x) -> decltype(x.first, o);
auto operator<<(auto& o, auto x) -> decltype(x.end(), o) {
  o << "{";
  for (int i = 0; auto y : x) o << ", " + !i++ * 2 << y;
  return o << "}"; }
auto operator<<(auto& o, auto x) -> decltype(x.first, o) {
  return o << "(" << x.first << ", " << x.second << ")"; }
void __print(auto... x) { ((cerr << x << " "), ...) << endl; }
#define debug(x...) __print("[" #x "]:", x)
#else
#define debug(...) 2137
#endif

mt19937 rng(2137);
struct Node {
	Node *l = 0, *r = 0;
	int val = 0, y;
	Node() : y(rng()) {}
};

void each(Node* n, auto f) {
	if (n) { each(n->l, f); f(n->val); each(n->r, f); }
}

pair<Node*, Node*> split(Node* n, int k) {
	if (!n) return {};
	if (n->val >= k) { // "n->val >= k" for lower_bound(k)
		auto [L,R] = split(n->l, k);
		n->l = R;
		return pair(L, n);
	} else {
		auto [L,R] = split(n->r,k); // and just "k"
		n->r = L;
		return pair(n, R);
	}
}

Node* merge(Node* l, Node* r) {
	if (!l || !r) return l ?: r;
	if (l->y > r->y) {
		l->r = merge(l->r, r);
		return l;
	} else {
		r->l = merge(l, r->l);
		return r;
	}
}

// Union of two sorted treaps, O(m log(n/m)) where m<=n
// Makes small-to-large O(n log n) instead of log^2.
// Requires lower_bound split (not the default one).
Node* unite(Node* a, Node* b) {
	if (!a || !b) return a ?: b;
	if (a->y < b->y) swap(a, b);
	auto [l, r] = split(b, a->val); // lower_bound split
  a->l = unite(l, a->l);
  a->r = unite(r, a->r);
  return a;
}

int peek(Node* a) {
  if (!a->l) return a->val;
  return peek(a->l);
}

const int N = 50050;
int n, m;
int a[N];
int odp[N];
Node t[N];

int main() {
  cin.tie(0)->sync_with_stdio(0);
  cin >> n >> m;
  rep(i, 0, n) cin >> a[i];
  rep(i, 0, n) t[i].val = i;
  int kto = n - 1;
  map<pii, pair<Node*, int>> pary;
  pary[{m, a[n - 1]}] = {&t[n - 1], 1};
  for (int i = n - 2; i >= 0; i--) {
    int wym = (n - i + 1) / 2 - (kto - i);
    map<int, int> ile;
    for (auto& [x, y] : pary) ile[x.first + x.second] += y.second;
    int juz = 0;
    int koszt = 0;
    int ost = -1;
    int usun = 0;
    for (auto [x, y] : ile) {
      if (juz == wym) break;
      int wez = min(y, wym - juz);
      juz += wez;
      koszt += wez * x;
      ost = x;
      usun = y - wez;
    }
    if (koszt <= m) {
      map<pii, pair<Node*, int>> pary2;
      for (auto& [x, y] : pary) {
        if (x.first + x.second <= ost) {
          pary2[{x.first + x.second, x.second}] = y;
        } else {
          auto& [r, s] = pary2[{0, x.second}];
          s += y.second;
          r = unite(r, y.first);
        }
      }
      // POCZATEK REMISOW
      priority_queue<pii> q;
      rep(j, 1, 65) if (pary2.count({ost, j})) {
        q.push({-peek(pary2[{ost, j}].first), j});
      }
      rep(j, 0, usun) {
        auto [x, y] = q.top(); q.pop(); x *= -1;
        auto& [r1, s1] = pary2[{ost, y}];
        auto [w, rr1] = split(r1, x + 1);
        r1 = rr1;
        s1--;
        if (s1) q.push({-peek(rr1), y});
        else pary2.erase({ost, y});
        auto& [r2, s2] = pary2[{0, a[x]}];
        s2++;
        r2 = unite(r2, w);
      }
      /*for (int j = remis._Find_first(); usun--; j = remis._Find_next(j)) {
        //int x = remis._Find_first();
        //remis[x] = 0;
        pary2[{ost, a[j]}][j] = 0;
        pary2[{0, a[j]}][j] = 1;
      }*/
      // KONIEC REMISOW
      rep(j, i + 1, kto) {
        auto& [r, s] = pary2[{0, a[j]}];
        r = unite(r, &t[j]);
        s++;
      }
      auto& [r, s] = pary2[{m - koszt, a[i]}];
      r = unite(r, &t[i]);
      s++;
      kto = i;
      pary = pary2;
    }
  }
  rep(i, 0, kto) odp[i] = -1;
  for (auto [x, y] : pary) {
    each(y.first, [&](int i) {
      odp[i] = x.first;
    });
  }
  rep(i, 0, n) cout << odp[i] << " \n"[i == n - 1];
}