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
#include<bits/stdc++.h>
#define fi first
#define se second
using namespace std;
const int N=1e5;
const long long INF=1e18L+7;
long long t[2][N+10];
vector<int> breaks;
struct Treap
{
	int pr;
	Treap *l,*r;
	int siz;
	long long val;
	long long ad,mx;
};
const int TSIZE=1e6;
int TIT=0;
Treap TMEM[TSIZE];
Treap cpTMEM[TSIZE];
int grt;
Treap* new_treap()
{
	TMEM[TIT].l=TMEM[TIT].r=nullptr;
	TMEM[TIT].siz=1;
	TMEM[TIT].val=INF;
	TMEM[TIT].ad=TMEM[TIT].mx=0;
	return &TMEM[TIT++];
}
Treap* rt;
int id(Treap *x,int lft)
{
	if(x==nullptr)
		return -1;
	return lft+(x->l==nullptr ? 0:x->l->siz)+1;
}
long long mid;
long long norm(long long x)
{
	x=max(0LL,x);
	if(x>=mid)
		x=INF;
	return x;
}
void recalc(Treap* x)
{
	x->siz=1+(x->l==nullptr ? 0:x->l->siz)+(x->r==nullptr ? 0:x->r->siz);
	return;
}
void pushdown(Treap* x)
{
	if(x==nullptr)
		return;
	for(auto y:{x->l,x->r})
	{
		if(y==nullptr)
			continue;
		y->ad+=x->ad;
		y->mx=max(x->mx,y->mx+x->ad);
	}
	x->val+=x->ad;
	x->val=max(x->val,x->mx);
	x->ad=x->mx=0;
	return; 
}
pair<Treap*,int> loc(int x)
{
	auto i=rt;
	int lft=0;
	while(id(i,lft)!=x)
	{
		//cerr<<id(i,lft)<<" "<<x<<"\n";
		pushdown(i);
		if(id(i,lft)<x)
		{
			lft=id(i,lft);
			i=i->r;
		}
		else
			i=i->l;
	}
	pushdown(i);
	return {i,lft};
}
long long read(int x)
{
	if(x<=0)
		return INF;
	auto [i,lft]=loc(x);
	return norm(i->val);
}
bool chckinf(int x,long long md)
{
	auto [i,lft]=loc(x);
	if(i->val>=md)
	{
		i->val=INF;
		return true;
	}
	return false;
}
void add(Treap *x,long long ad1,long long mx1)
{
	x->ad+=ad1;
	x->mx=max(x->mx+ad1,mx1);
	return;
}
pair<Treap*,Treap*> split(Treap* x,int c,int lft)
{
	if(x==nullptr)
		return {nullptr,nullptr};
	pushdown(x);
	if(id(x,lft)<=c)
	{
		auto [a,b]=split(x->r,c,id(x,lft));
		x->r=a;
		recalc(x);
		return {x,b};
	}
	auto [a,b]=split(x->l,c,lft);
	x->l=b;
	recalc(x);
	return {a,x};
}
Treap* merge(Treap* x,Treap* y)
{
	pushdown(x);
	pushdown(y);
	if(x==nullptr)
		return y;
	if(y==nullptr)
		return x;
	if(x->pr<y->pr)
	{
		x->r=merge(x->r,y);
		recalc(x);
		return x;
	}
	y->l=merge(x,y->l);
	recalc(y);
	return y;
}
int ff,ll;
int ff2;
void find_ff2(long long md,int i)
{
	ff2=ff;
	while(ff2<=ll && norm(read(ff2)+t[1][i])>=md && norm(read(ff2-1)+t[0][i])>=md)
		ff2++;
	ff2--;
	//cerr<<"ff2="<<ff2<<"\n";
	return;
}
pair<Treap*,int> fnd(Treap *x,int lft,int i)
{
	if(x==nullptr)
		return {nullptr,-1};
	pushdown(x);
	//cerr<<id(x,lft)<<"x\n";
	if(id(x,lft)<=ff2 || norm(x->val+t[1][i])<norm(read(id(x,lft)-1)+t[0][i]))
	{
		auto c=fnd(x->r,id(x,lft),i);
		if(c.se!=-1)
			return c;
		return {x,id(x,lft)};
	}
	return fnd(x->l,lft,i);
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);
	srand(2193203);
	for(int i=0;i<TSIZE;i++)
		TMEM[i].pr=rand();
	int n,k;
	cin>>n>>k;
	for(int j:{0,1})
	{
		for(int i=1;i<=n;i++)
			cin>>t[j][i];
	}

	rt=new_treap();
	rt->val=0;
	grt=0;
	for(int i=1;i<=k;i++)
	{
		rt=merge(rt,new_treap());
		if(TMEM[i].pr<TMEM[grt].pr)
			grt=i;
	}
	for(int i=0;i<=k;i++)
		cpTMEM[i]=TMEM[i];

	auto solve=[&]()
	{
		breaks.clear();

		for(int i=0;i<=k;i++)
			TMEM[i]=cpTMEM[i];
		TIT=k+1;
		rt=&TMEM[grt];

		ff=1,ll=k+1;
		for(int i=1;i<=n;i++)
		{
			//cerr<<i<<"\n";
			find_ff2(mid,i);
			auto [x,it]=fnd(rt,0,i);
			breaks.push_back(it);
			//cerr<<it<<"\n";
			pushdown(x);
			auto [a,b]=split(rt,it,0);
			auto v=new_treap();
			v->val=x->val;
			b=merge(v,b);
			add(a,t[1][i],0);
			add(b,t[0][i],0);
			rt=merge(a,b);

			ll++;
			while(ff<=ll && chckinf(ff,mid))
				ff++;
			while(ff<=ll && chckinf(ll,mid))
				ll--;
			//cerr<<"ff="<<ff<<" ll="<<ll<<"\n";
			//cerr<<"read:\n";
			//for(int j=1;j<=k+1;j++)
			//	cerr<<read(j)<<" \n"[j==k+1];
		}

		return read(k+1);
	};

	long long bg=0,en=(1e9L+7)*n;
	while(bg<en)
	{
		mid=(bg+en+1)/2;
			
		if(solve()>=mid)
			bg=mid;
		else
			en=mid-1;
		//return 0;
	}

	mid=bg+1;
	solve();
	cout<<bg<<"\n";
	reverse(breaks.begin(),breaks.end());
	string s;
	k++;
	for(auto v:breaks)
	{
		if(k>v)
		{
			s+='A';
			k--;
		}
		else
			s+='B';
	}
	reverse(s.begin(),s.end());
	cout<<s<<"\n";
	return 0;
}