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
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
using namespace std;
#define y0 y0z
#define y1 y1z
#define yn ynz
#define j0 j0z
#define j1 j1z
#define jn jnz
#define tm tmz
#define buli(x) (__builtin_popcountll(x))
#define bur0(x) (__builtin_ctzll(x))
#define bul2(x) (63-__builtin_clzll(x))
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define siz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<_ed;i++)
#define per(i,a,b) for (int i=(b)-1,_ed=(a);i>=_ed;i--)
#define forg(i,gu) for (int i=gu;~i;i=e[i].next)
#define pw(x) ((ll(1))<<(x))
#define upmo(a,b) (((a)=((a)+(b))%mo)<0?(a)+=mo:(a))
#define mmo(a,b) (((a)=1ll*(a)*(b)%mo)<0?(a)+=mo:(a))
void getre(){int x=0;printf("%d\n",1/x);}
void gettle(){int res=1;while(1)res<<=1;printf("%d\n",res);}
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<pii> vpii;
template<typename T,typename S>inline bool upmin(T&a,const S&b){return a>b?a=b,1:0;}
template<typename T,typename S>inline bool upmax(T&a,const S&b){return a<b?a=b,1:0;}
template<typename N,typename PN>inline N flo(N a,PN b){return a>=0?a/b:-((-a-1)/b)-1;}
template<typename N,typename PN>inline N cei(N a,PN b){return a>0?(a-1)/b+1:-(-a/b);}
template<typename N>N gcd(N a,N b){return b?gcd(b,a%b):a;}
template<typename N>inline int sgn(N a){return a>0?1:(a<0?-1:0);}
#if ( ( _WIN32 || __WIN32__ ) && __cplusplus < 201103L)
#define lld "%I64d"
#else
#define lld "%lld"
#endif
inline void gn(long long&x){
	int sg=1;char c;while(((c=getchar())<'0'||c>'9')&&c!='-');c=='-'?(sg=-1,x=0):(x=c-'0');
	while((c=getchar())>='0'&&c<='9')x=x*10+c-'0';x*=sg;
}
inline void gn(int&x){long long t;gn(t);x=t;}
inline void gn(unsigned long long&x){long long t;gn(t);x=t;}
inline void gn(double&x){double t;scanf("%lf",&t);x=t;}
inline void gn(long double&x){double t;scanf("%lf",&t);x=t;}
inline void gs(char *s){scanf("%s",s);}
inline void gc(char &c){while((c=getchar())>126 || c<33);}
inline void pc(char c){putchar(c);}
#ifdef JCVB
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif
typedef long long ll;
typedef double db;
inline ll sqr(ll a){return a*a;}
inline db sqrf(db a){return a*a;}
//const int inf=0x3f3f3f3f;
const ll inf=0x3f3f3f3f3f3f3f3fll;
const db pi=3.14159265358979323846264338327950288L;
const db eps=1e-6;
//const int mo=0;
//int qp(int a,ll b){int n=1;do{if(b&1)n=1ll*n*a%mo;a=1ll*a*a%mo;}while(b>>=1);return n;}

const int N = 111111;

ll actsum[N];
ll buf[N];
// range update, range query
// index 1..n
// first call seginit(n)

typedef ll seg_nu;
typedef ll seg_tag;
const int SEG_MAXN=100000+5;

int seglen[SEG_MAXN*4];
seg_nu seg[SEG_MAXN*4];
seg_tag tag[SEG_MAXN*4];
seg_tag tag0 = 0;						//modify
inline void segadd(int x,seg_tag v){
	seg[x]=seg[x]+v;				//modify
	tag[x]=tag[x]+v;					//modify
}
inline void segpd(int x){
	if(tag[x]!=tag0){
		segadd(x<<1,tag[x]);
		segadd(x<<1|1,tag[x]);
		tag[x]=tag0;					
	}
}
inline void segpu(int x){
	seg[x]=max(seg[x<<1],seg[x<<1|1]);				//modify
}
void seginit_in(int l,int r,int x){
	tag[x]=tag0;						
	seglen[x]=r-l+1;
	if(l==r){
		seg[x]=actsum[l];
		//seg[x]=0;					//modify
	}else{
		int mid=l+r>>1;
		seginit_in(l,mid,x<<1);
		seginit_in(mid+1,r,x<<1|1);
		segpu(x);
	}
}
int l1,r1;
seg_nu sans; bool ans_bo;
seg_tag stag;
void segupd_in(int l,int r,int x){
	//if(l1>r1)return;
	if(l1<=l && r<=r1){
		segadd(x,stag);
	}else{
		int mid=l+r>>1;
		segpd(x);
		if(l1<=mid)segupd_in(l,mid,x<<1);
		if(r1>mid)segupd_in(mid+1,r,x<<1|1);
		segpu(x);
	}
}
void segque_in(int l,int r,int x){
	//if(l1>r1)return;
	if(l1<=l && r<=r1){
		if(!ans_bo)ans_bo=1,sans=seg[x];
		else sans=max(sans,seg[x]);				//modify
	}else{
		int mid=l+r>>1;
		segpd(x);
		if(l1<=mid)segque_in(l,mid,x<<1);
		if(r1>mid)segque_in(mid+1,r,x<<1|1);
	}
}
int segn;
void segupd(int l,int r,seg_tag v){
	if(l>r)return;
	stag=v,l1=l,r1=r;
	segupd_in(1,segn,1);
}
seg_nu segque(int l,int r){
	if(l>r)return -inf;					//modify
	ans_bo=0,l1=l,r1=r;
	segque_in(1,segn,1);
	return sans;
}
void seginit(int n){
	segn=n;
	seginit_in(1,segn,1);
}

int n,k;
int a[N],b[N];
int x[N],can[N],y[N];
int need;

ll s[N];
int canarr[N];
int cantot;
int cmp(int i,int j) {
	return y[i]<y[j];
}

int nowx[N];
ll nows[N];

//////////////
ll sumbuf[4*N];

void sumbuf_init(int l,int r,int x){
	if(l==r){
		sumbuf[x]=buf[l];
	}else{
		int mid=l+r>>1;
		sumbuf_init(l,mid,x<<1);
		sumbuf_init(mid+1,r,x<<1|1);
		sumbuf[x]=sumbuf[x<<1]+sumbuf[x<<1|1];
	}
}
ll sumbuf_que(int l,int r,int x) {
	if(l1<=l && r<=r1) {
		return sumbuf[x];
	} else {
		int mid=l+r>>1;
		ll ret=0;
		if(l1<=mid)ret+=sumbuf_que(l,mid,x<<1);
		if(r1>mid)ret+=sumbuf_que(mid+1,r,x<<1|1);
		return ret;
	}
}
int sI;ll del;// del should be negativ
void sumbuf_add(int l,int r,int x) {
	if(l==r) {
		sumbuf[x]+=del;
	} else {
		int mid=l+r>>1;
		if(sI<=mid)sumbuf_add(l,mid,x<<1);
		else sumbuf_add(mid+1,r,x<<1|1);
		
		sumbuf[x]+=del;
	}
}

// >=I first
pair<int,ll> sumbuf_nex(int l,int r,int x) {
	if(sumbuf[x]==0)return mp(-1,-1);
	if(l==r) {
		return mp(l,sumbuf[x]);
	} else {
		int mid=l+r>>1;
		pair<int,ll> ret=mp(-1,-1);
		if(sI<=mid) ret = sumbuf_nex(l,mid,x<<1);
		if(ret.fi!=-1)return ret;
		ret = sumbuf_nex(mid+1,r,x<<1|1);
		return ret;
	}
}
////////////

bool picked[N];
int ok(ll lim) {
	rep(i,1,n+1)nowx[i] = x[i];
	nows[0]=buf[0]=actsum[0]=0;
	rep(t,1,n+1) {
		nows[t] =max(0ll,nows[t-1]+nowx[t]);
		buf[t] = nows[t] - (nows[t-1]+nowx[t]); //0 or positive
		actsum[t] = actsum[t-1] + nowx[t];
	}
	seginit(n);
	sumbuf_init(1,n,1);
	int chosen=0;
	rep(j,1,cantot+1) {
		int i = canarr[j];
		if(chosen>=need) {
			picked[i]=0;
		} else {
			ll maa = segque(i,n);
			if(i!=1) {
				l1=1,r1=i-1;
				maa += sumbuf_que(1,n,1);
			}
			maa += y[i];

			if(maa>lim) {
				picked[i]=0;
			} else {
				picked[i]=1;
				chosen++;
				segupd(i,n,y[i]);

				ll rem=y[i];
				int cur = i;
				while(rem>0 && cur!=-1) {
					sI = cur;
					pair<int,ll> an = sumbuf_nex(1,n,1);
					if(an.fi==-1)break;
					cur=an.fi;
					ll de = min(an.se, rem);
					rem-=de;
					del = -de;
					sI = an.fi;
					sumbuf_add(1,n,1);
				}
			}
		}
	}
	return chosen>=need;
}

char out[N];

ll fafa[N];
int main()
{
#ifdef JCVB
	//freopen("1.in","r",stdin);
	//freopen("1.out","w",stdout);
	int _time_jc=clock();
#endif
	gn(n);gn(k);
	rep(i,1,n+1)gn(a[i]);
	rep(i,1,n+1)gn(b[i]);
	int numada=0;
	rep(i,1,n+1)numada += a[i]>b[i];
	if(n-k<=numada) {
		need = numada - (n-k);
		rep(i,1,n+1) if(a[i]>b[i]) {
			x[i] = b[i];
			y[i] = a[i]-b[i];
			can[i]=1;
		} else {
			x[i]=a[i];
			can[i]=0;
			out[i]='A';
		}
	} else {
		need = (n-numada) - k;
		rep(i,1,n+1) if(a[i]>b[i]) {
			x[i]=b[i];
			can[i]=0;
			out[i]='B';
		} else {
			x[i]=a[i];
			y[i] = b[i]-a[i];
			can[i]=1;
		}
	}

	cantot=0;
	rep(i,1,n+1)if(can[i]) {
		canarr[++cantot]= i;
	}
	sort(canarr+1,canarr+1+cantot,cmp);

	s[0]=0;
	ll lo=0;
	rep(i,1,n+1) {
		s[i] = max(0ll, s[i-1]+x[i]);
		upmax(lo,s[i]);
	}
	ll hi = 1ll*1.1e9 * n;
	while(lo<=hi) {
		ll mid = (lo+hi)>>1;
		if(ok(mid))hi=mid-1;
		else lo=mid+1;
	}
	ok(lo);
	rep(i,1,n+1)if(can[i]==1) {
		if(n-k<=numada) {
			if(picked[i]) {
				out[i]='A';
			} else out[i]='B';
		}else {
			if(picked[i]) {
				out[i]='B';
			} else out[i]='A';
		}
	}
	cout<<lo<<endl;

	/*int na=0;
	rep(i,1,n+1)if(out[i]=='A')na++;
	assert(na==k);
	rep(i,1,n+1)if(out[i]=='A')fafa[i]=a[i];else fafa[i]=b[i];
	ll maaaa=0;
	rep(i,1,n+1){
		s[i] = max(0ll,s[i-1]+fafa[i]);
		upmax(maaaa,s[i]);
	}
	assert(maaaa==lo);*/
	rep(i,1,n+1)putchar(out[i]);
	putchar('\n');
#ifdef JCVB
	debug("time: %d\n",int(clock()-_time_jc));
#endif
	return 0;
}