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
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
 
using namespace __gnu_pbds;
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
typedef double db;
mt19937_64 mrand(1);
const ll mod=1000000007;
int rnd(int x) { return mrand() % x;}
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head


const int N=101000;
int n,k;
int a[N],b[N];
ll dp[N],l,r;
ll inf=1ll<<60;

bool check(ll md) {
	dp[0]=0;
	int L=0,R=0;
	ll vL=0,vR=0,minv=0;
	multiset<int> neg,pos;
	rep(i,0,n) {
		if (a[i]-b[i]>0) pos.insert(a[i]-b[i]);
		else neg.insert(b[i]-a[i]);
		minv+=min(a[i],b[i]);
		vL+=b[i]; vR+=a[i]; ++R;
		if (minv<0) {
			auto upd=[&](multiset<int> &pos,ll &vR) {
				ll v=-minv;
				while (v) {
					if (pos.empty()) { vR=0; break; }
					if (*pos.begin()<v) {
						v-=*pos.begin();
						pos.erase(pos.begin());
					} else {
						int d=*pos.begin()-v;
						pos.erase(pos.begin()); if (d) pos.insert(d);
						break;
					}
				}
			};
			upd(pos,vR);
			upd(neg,vL);
			minv=0;
		}
		if (minv>md) return 0;
		while (vL>md) {
			assert(!neg.empty());
			vL-=*(neg.rbegin());
			neg.erase(--neg.end());
			L++;
		}
		while (vR>md) {
			assert(!pos.empty());
			vR-=*(pos.rbegin());
			pos.erase(--pos.end());
			R--;
		}
	}
	return k>=L&&k<=R;
}


typedef tree<PII,null_type,less<PII>,rb_tree_tag,tree_order_statistics_node_update> ordered_set;
int p[N];
char ret[N];
void construct(ll md) {
	int L=0,R=0;
	ll vL=0,vR=0,minv=0;
	ordered_set gap;
	rep(i,0,n) {
		PII np(a[i]-b[i],i);
		gap.insert(np);
		/*p[i]=L;
		fprintf(stderr,"%d\n",i);
		for (auto x:gap) {
			if (x==np) break;
			p[i]++;
		}*/
		p[i]=gap.order_of_key({a[i]-b[i],i})+L;
		minv+=min(a[i],b[i]);
		vL+=b[i]; vR+=a[i]; ++R;
		if (minv<0) {
			ll v=-minv;
			while (v) {
				auto it=gap.upper_bound({1,-1});
				if (it==gap.end()) { vR=0; break; }
				ll d=min((ll)it->fi,v);
				PII np(it->fi-d,it->se);
				v-=d; gap.erase(it); gap.insert(np);
			}
			v=-minv;
			while (v) {
				auto it=gap.lower_bound({0,-1});
				if (it==gap.begin()) { vL=0; break; }
				--it;
				ll d=min(-(ll)it->fi,v);
				PII np(it->fi+d,it->se);
				v-=d; gap.erase(it); gap.insert(np);
			}
			minv=0;
		}
		while (vL>md) {
			vL+=gap.begin()->fi;
			gap.erase(gap.begin());
			L++;
		}
		while (vR>md) {
			vR-=gap.rbegin()->fi;
			gap.erase(--gap.end());
			R--;
		}
	}
	int v=k;
	per(i,0,n) {
		if (p[i]>=v) ret[i]='B';
		else {
			ret[i]='A';
			--v;
		}
	}
	int ca=0;
	ll val=0,ans=0;
	rep(i,0,n) {
		assert(ret[i]=='A'||ret[i]=='B');
		if (ret[i]=='A') ca++;
		val=max(0ll,val+(ret[i]=='A'?a[i]:b[i]));
		ans=max(ans,val);
	}
	assert(ans==md);
	assert(ca==k);
	printf("%s\n",ret);
}

int main() {
	scanf("%d%d",&n,&k);
	rep(i,0,n) scanf("%d",a+i);
	rep(i,0,n) {
		scanf("%d",b+i);
		r+=max(max(a[i],b[i]),0);
	}
	l=-1;
	while (l+1<r) {
		ll md=l+(r-l)/2;
		if (check(md)) r=md; else l=md;
	}
	printf("%lld\n",r);
	construct(r);
}