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

#define ll long long
#define ld long double
#define endl '\n'
#define st first
#define nd second
#define pb push_back
#define sz(x) (int)(x).size()
#define all(x) (x).begin(), (x).end()
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define ROF(i,r,l) for(int i=(r);i>=(l);i--)
using namespace std;
ll infl=1000000000000000007;
int inf=1000000007;

#ifdef DEBUG
auto&operator<<(auto&o,pair<auto,auto>p){return o<<"("<<p.first<<", "<<p.second<<")";}
auto operator<<(auto&o,auto x)->decltype(x.end(),o){o<<"{";int i=0;for(auto e:x)o<<","+!i++<<e;return o<<"}";}
#define debug(X...)cerr<<"["#X"]: ",[](auto...$){((cerr<<$<<"; "),...)<<endl;}(X)
#else
#define debug(...){}
#endif

const int pot=1<<16;
const int N=50007;

int a[N];
int ans[N];

struct node
{
	int ile=0;
	node *L=NULL;
    node *R=NULL;
};

void merge(node *v,node *u)
{
	v->ile+=u->ile;
	debug(v->ile,u->ile);
	if(u->L!=NULL)
	{
		if(v->L!=NULL) merge(v->L,u->L);
		else v->L=u->L;
	}
	if(u->R!=NULL)
	{
		if(v->R!=NULL) merge(v->R,u->R);
		else v->R=u->R;
	}
}
void ins(node *v,int l,int r,int x)
{
	v->ile++;
	if(l==r) 
	{
		debug(l,x);
		return ;
	}
	int m=(l+r)/2;
	if(x<=m) 
	{
		if(v->L==NULL) v->L=new node;
		ins(v->L,l,m,x);
	}
	else
	{
		if(v->R==NULL) v->R=new node;
		ins(v->R,m+1,r,x);
	}
}
void split(node *v,node *u,int l,int r,int x)
{
	if(x<l||l==r) return ;
	int m=(l+r)/2;
	if(x>=m)
	{
		u->L=v->L;
		v->L=NULL;
		if(v->R!=NULL) 
		{
			u->R=new node;
			split(v->R,u->R,m+1,r,x);
		}
	}
	else
	{
		if(v->L!=NULL)
		{
			u->L=new node;
			split(v->L,u->L,l,m,x);
		}
	}
	u->ile=0;
	if(u->L!=NULL) u->ile+=u->L->ile;
	if(u->R!=NULL) u->ile+=u->R->ile;
	v->ile=0;
	if(v->L!=NULL) v->ile+=v->L->ile;
	if(v->R!=NULL) v->ile+=v->R->ile;
}

set<pair<int,node*>>S[70];
int OF[70];

node *root[70];

struct node* Newnode()
{
    struct node* xd = new struct node;
    xd->ile=0;
    xd->L=NULL;
    xd->R=NULL;
    return xd;
}

void add(int j,int x,int i)
{
	set<pair<int,node*>>::iterator it=S[j].lower_bound({x,NULL});
	if(it==S[j].end()||(*it).st>x) 
	{
		node *v=new node;
		ins(v,1,pot,i);
		S[j].insert({x,v});
	}
	else ins((*it).nd,1,pot,i);
}

int get(vector<node*>V,int k,int l,int r)
{
	if(l==r) return l;
	int sum=0;
	for(auto v:V) if(v->R!=NULL) sum+=v->R->ile;
	debug(l,r,sum);
	vector<node*>nV;
	if(sum>=k)
	{
		for(auto v:V) if(v->R!=NULL) nV.pb(v->R);
		return get(nV,k,(l+r)/2+1,r);
	}
	else
	{
		for(auto v:V) if(v->L!=NULL) nV.pb(v->L);
		return get(nV,k-sum,l,(l+r)/2);
	}
}


void dfs(node* v,int l,int r,int val)
{
	if(l==r)
	{
		if(v->ile==1) ans[l]=val;
		debug(l,val);
		return ;
	}
	if(v->L!=NULL) dfs(v->L,l,(l+r)/2,val);
	if(v->R!=NULL) dfs(v->R,(l+r)/2+1,r,val);
}

signed main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    int n,m;
    cin>>n>>m;
    //n=50000;
   // m=rand()%1000000+1;
    FOR(i,1,n) 
    {
		cin>>a[i];
		//a[i]=rand()%64+1;
	}
    ROF(i,n,1)
    {
		FOR(j,1,64) OF[j]+=j;
		vector<int>T(65,0);
		FOR(j,1,64) 
		{
			for(auto [x,v]:S[j])
			{
				debug(j,x+OF[j]);
				if(x+OF[j]>64) break;
				T[x+OF[j]]+=v->ile;
			}
		}
		int ile=0,sum=0,x,f=(n-i)/2;
		FOR(j,0,64)
		{
			if(ile+T[j]>=f)
			{
				sum+=(f-ile)*j;
				x=j;
				break;
			}
			ile+=T[j];
			sum+=T[j]*j;
		}
		debug(T);
		debug(sum,x);
		if(sum>m) 
		{
			add(a[i],-OF[a[i]],i);
			FOR(j,1,64) OF[j]-=j;
		}
		else
		{
			FOR(j,1,64)
			{
				root[j]=NULL;
				if(sz(S[j])>0&&(*S[j].begin()).st+OF[j]==0) root[j]=(*S[j].begin()).nd;
				while(sz(S[j])>0&&(*--S[j].end()).st+OF[j]>x)
				{
					if(root[j]==NULL)
					{
						root[j]=new node;
						S[j].insert({-OF[j],root[j]});
					}
					merge(root[j],(*--S[j].end()).nd);
					S[j].erase(--S[j].end());
				}
			}
			if(x>0)
			{
				vector<pair<node*,int>>V;
				vector<node*>R;
				FOR(j,1,64)
				{
					if(sz(S[j])>0&&(*--S[j].end()).st+OF[j]==x) 
					{
						V.pb({(*--S[j].end()).nd,j});
						R.pb((*--S[j].end()).nd);
					}
				}
				int r=get(R,f-ile,1,pot);
				debug(r);
				for(auto [v,j]:V) 
				{
					node *u=new node;
					split(v,u,1,pot,r-1);
					debug(j,u->ile);
					if(root[j]==NULL)
					{
						root[j]=new node;
						S[j].insert({-OF[j],root[j]});
					}
					merge(root[j],u);
				}
			}
			add(a[i],m-sum-OF[a[i]],i);
		}
	}
	FOR(j,1,64) for(auto [x,v]:S[j]) dfs(v,1,pot,x+OF[j]);
    FOR(i,1,n)
    {
		if(ans[i]<0) cout<<-1<<" ";
		else cout<<ans[i]<<" ";
	}
	cout<<endl;
    
    return 0;
}