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
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second

const int N = 100100;

int n,k;
int a[N], b[N];

int m;
pii vals[N];


bool flipped;
char ans[N];

int t[N];
ll getsolution() {
	ll ret = 0;
	ll sum = 0, bestpref = 0;
	FOR(i,n) {
		sum += t[i];
		ret = max(ret, sum-bestpref);
		bestpref = min(bestpref, sum);
	}
	return ret;
}

const ll infll = 1000100100LL * 1000100100LL;
const int NN = 5010;
ll dp[NN][NN];
int prv[NN][NN];
bool ok(ll solution, bool update_ans = false) {
	//cerr << "\nchecking " << solution << "\n";
	FOR(i,n+1) FOR(j,k+1) dp[i][j] = infll;
	dp[0][0] = 0;
	FOR(j,k+1) FOR(i,n) {
		ll h = dp[i][j];
		if (h+a[i] <= solution && h+a[i] < dp[i+1][j]) {
			dp[i+1][j] = max(0LL,h+a[i]);
			prv[i+1][j] = 0;
		}
		if (b[i]>0 && h+a[i]+b[i] <= solution && h+a[i]+b[i] < dp[i+1][j+1]) {
			dp[i+1][j+1] = max(0LL,h+a[i]+b[i]);
			prv[i+1][j+1] = 1;
		}
	}
	if (update_ans) {
		int nn = n, kk = k;
		while (nn > 0) {
			int pp = prv[nn][kk];
			if (pp) ans[nn-1] = 'A';
			nn--;
			kk-=pp;
		}
	}
	//if (solution == 379179) {
	//	FOR(i,n+1) {
	//		//printf("%d ", i);
	//		FOR(j,k+1) printf("%lld ", (dp[i][j]==infll ? solution+3 : dp[i][j]));
	//		printf("\n");
	//	}
	//}
	//cerr << "there are " << SZ(dp[n][0]) << " elements so solution = " << solution << " is " << (dp[n][0].empty() ? "not " : "") << "ok\n";
	return dp[n][k] < infll;
}

int main() {
	scanf("%d%d", &n, &k);
	FOR(i,n) scanf("%d", &a[i]);
	FOR(i,n) scanf("%d", &b[i]);
	
	int cA = 0, cB = 0;
	FOR(i,n) {
		cA += a[i] < b[i];
		cB += a[i] > b[i];
		t[i] = min(a[i], b[i]);
	}
	int cS = n-cA-cB;
	ll minsolution = getsolution();
	
	/// easy case: taking min is solution
	if (cA <= k && k <= cA+cS) {
		cS = k-cA;
		FOR(i,n) {
			if (a[i] < b[i]) ans[i] = 'A';
			else if (a[i] > b[i]) ans[i] = 'B';
			else {
				if (cS > 0) ans[i] = 'A';
				else ans[i] = 'B';
				cS--;
			}
		}
		printf("%lld\n%s\n", minsolution, ans);
		return 0;
	}
	
	/// flip solution
	if (cA > k) {
		flipped = 1;
		FOR(i,n) swap(a[i], b[i]);
		swap(cA,cB);
		k = n-k;
	}
	
	/// reduce
	FOR(i,n) {
		if (a[i] <= b[i]) {
			ans[i] = 'A';
			b[i] = 0;
		} else {
			ans[i] = 'B';
			swap(a[i], b[i]);
			b[i] = b[i]-a[i];
			vals[m++] = mp(b[i], i);
		}
	}
	k -= cA + cS;
	sort(vals, vals+m);
	
	/// worst case
	FOR(i,n) t[i] = a[i]+b[i];
	ll maxsolution = getsolution();
	if (maxsolution <= 0) {
		FOR(i,n) if (k>0 && b[i]>0) {
			ans[i] = 'A';
			a[i] += b[i];
			k--;
		}
		if (flipped) FOR(i,n) {
			if (ans[i] == 'A') ans[i] = 'B';
			else ans[i] = 'A';
		}
		printf("0\n%s\n", ans);
		return 0;
	}
	
	//cerr << "new problem is: flip at least " << k << " with\nbase = ";
	//FOR(i,n) cerr << a[i] << " ";
	//cerr << "\nadd  = ";
	//FOR(i,n) cerr << b[i] << " ";
	//cerr << "\n";
	//cerr << "solution is in [" << minsolution << ", " << maxsolution << "]\n";
	
	/// find score
	minsolution--;
	while (minsolution + 1 < maxsolution) {
		ll midsolution = (minsolution + maxsolution) / 2;
		if (ok(midsolution)) {
			maxsolution = midsolution;
		} else {
			minsolution = midsolution;
		}
	}
	
	/// find answer
	ok(maxsolution, true);
	
	/// flip and print
	if (flipped) FOR(i,n) {
		if (ans[i] == 'A') ans[i] = 'B';
		else ans[i] = 'A';
	}
	printf("%lld\n%s\n", maxsolution, ans);
	return 0;
}