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
#include "bits/stdc++.h"
using namespace std;
#define all(x) x.begin(),x.end()
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) { return os << p.first << " " << p.second; }
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) { string sep; for (const T &x : v) os << sep << x, sep = " "; return os; }
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#define ASSERT(...) 42
#endif
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pi;
const int oo = 1e9;
struct node {
    int mn;
    
    void merge(const node& o) {
        mn=min(mn,o.mn);
    }
    friend node merge(node a, const node& b) {
        a.merge(b);
        return a;
    }
    void putval(const node& nd) {
    }
    void stopLazy() {
    }
};
#ifdef LOCAL
std::mt19937 rng(69);
#else
std::mt19937 rng(std::chrono::steady_clock::now().time_since_epoch().count());
#endif
template<class I> I rnd(I l,I r){return std::uniform_int_distribution<I>(l,r)(rng);}

struct treap {
    int priority,sz=1;
    node nd = {};
    // sub = {};
    treap *l=NULL, *r=NULL;
    treap(){}
    treap(node a) : nd(a) {
        priority = rng();
    }
    // friend node get(treap* nd) { return nd?nd->sub:node(); }
    friend int size(treap* nd) { return nd?nd->sz:0; }
    void recalc() {
        // sub=merge(merge(get(l),nd), get(r));
        // sub.stopLazy();
        sz=size(l)+1+size(r);
    }
    void putval(const node& a) { nd.putval(a); }
    // void print(int lazy=0) { // don't disturb the tree.
    //     if(l) l->print(lazy+sub.lazy);
    //     cout << nd.lazy+lazy << ' ';
    //     if(r) r->print(lazy+sub.lazy);
    // }
    void push() {
        // if(l) l->putval(sub);
        // if(r) r->putval(sub);
        // sub.stopLazy();
    }
};
treap* concat(treap* a, treap* b) { // just concatenates the sequences of a and b.
    if(!a or !b) return a?a:b;
    if(a->priority>b->priority) {
        a->push();
        a->r = concat(a->r,b);
        a->recalc();
        return a;
    } else {
        b->push();
        b->l = concat(a,b->l);
        b->recalc();
        return b;
    }
}
array<treap*,2> split(treap* a, int k) { // k in left halve
    if(!a) return {NULL,NULL};
    a->push();
    if(size(a->l)>=k) {
        auto [l,r] = split(a->l,k);
        a->l=r;
        a->recalc();
        return {l,a};
    } else {
        auto [l,r] = split(a->r,k-size(a->l)-1);
        a->r=l;
        a->recalc();
        return {a,r};
    }
}
array<treap*,2> splitByVal(treap* a, int val) { // <val in left halve.
    if(!a) return {NULL,NULL};
    a->push();
    if(a->nd.mn>=val) {
        auto [l,r] = splitByVal(a->l,val);
        a->l=r;
        a->recalc();
        return {l,a};
    } else {
        auto [l,r] = splitByVal(a->r,val);
        a->r=l;
        a->recalc();
        return {a,r};
    }
}
treap* merge(treap* a, treap* b) { // merges two treaps with sorted keys, such that in the end you have a treap with all keys sorted.
	if (!a || !b) { return a ? a : b; }
    a->push();
    b->push();
	if (a->priority > b->priority) {
		auto [c,d] = splitByVal(b, a->nd.mn);
		a->l = merge(a->l, c);
		a->r = merge(a->r, d);
		a->recalc();
        return a;
	} else {
		auto [c,d] = splitByVal(a, b->nd.mn);
		b->l = merge(c, b->l);
		b->r = merge(d, b->r);
		b->recalc();
        return b;
	}
}
void trav(treap* at, auto f) {
    if(!at) return;
    f(at);
    trav(at->l,f);
    trav(at->r,f);
}
int leq(treap* rt, int v) { // number of nodes <= than v.
    if(!rt) return 0;
    // #warning specific to this problem.
    rt->push();
    if(rt->nd.mn<=v) {
        return size(rt->l)+1+leq(rt->r,v);
    } else return leq(rt->l,v);
}
const int A = 65;
const int M = A*2;
struct bucket {
    treap* pera[A] = {};
    int sz=0;
    pair<bucket,bucket> get(int s, int lo, int hi) {
        while(lo<hi) {
            int mid = (lo+hi+1)/2;
            int num=0;
            for(int j=0;j<A;++j) {
                num+=size(pera[j]) - leq(pera[j],mid-1);
                if(num>=s) break;
            }
            if(num>=s) {
                lo = mid;
            } else hi = mid-1;
        }
        bucket x,y;
        for(int i=0;i<A;++i) {
            auto [c,d] = splitByVal(pera[i],lo);
            x.pera[i] = c;
            y.pera[i]=d;
        }
        return make_pair(x,y);
        
        
    }
    void give(int ai, treap* tr) {
        sz+=size(tr);
        pera[ai] = merge(pera[ai],tr);
    }
    
};
const int N = 1e5;
treap mem[N];
treap* s = mem;
auto newtreap(treap tp) {
    *s = tp;
    return s++;
}
int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,m; cin >> n >> m;
    vi a(n);
    for(auto& i : a) cin >> i;
    int overboard=0;
    vector<bucket> buckets(M);
    for(int i=n-1;i>=0;--i) {
        vi ord;
        int take = (n-i+1)/2-1;
        take=max(0,take);
        int left = m;
        
        int at=0;
        vi use(M);

        while(take>0) {
            if(buckets[at].sz<take) {
                use[at]=buckets[at].sz;
                // just take them all
                left-=buckets[at].sz*at;
                take-=buckets[at].sz;
                at++;

            } else {
                left-=take*at;
                use[at]=take;
                break;
            }
        }
        int ii = i;
        if(left<0) {
            overboard++;
            buckets[0].give(a[i], newtreap(treap({i})));
        } else {
            vector<bucket> nbuckets(M);

            for(int i=0;i<M;++i) {
                if(use[i]==buckets[i].sz) {
                    for(int o=0;o<A;++o) if(buckets[i].pera[o]) {
                        nbuckets[min(o+i,M-1)].give(o,buckets[i].pera[o]);
                    }
                } else if(use[i]==0) {
                    for(int o=0;o<A;++o) if(buckets[i].pera[o]) {
                        nbuckets[o].give(o,buckets[i].pera[o]);
                    }
                } else {
                    auto [x,y] = buckets[at].get(use[i],ii,n-1);
                    for(int o=0;o<A;++o) {
                        nbuckets[o].give(o,x.pera[o]);
                        nbuckets[min(M-1,o+i)].give(o,y.pera[o]);
                    }
                }
            }
            nbuckets[min(left+a[i],M-1)].give(a[i],newtreap({node{i}}));
            swap(buckets,nbuckets);
            overboard=0;
        }
    }
    vi d(n);
    // traverse them all!
    for(int i=0;i<M;++i) for(int j=0;j<A;++j) {
        trav(buckets[i].pera[j],[&](treap* tp) {
            d[tp->nd.mn]=i-j;
        });
    }
    // make it sum to d!
    int sum=m;
    for(int i=overboard+1;i<n;++i) {
        sum-=d[i];
    }
    d[overboard]=sum;
    for(int j=0;j<overboard;++j) d[j]=-1;
    cout << d << ' '<<'\n';
    

}