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
#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef double K;
constexpr int INF = 0x3f3f3f3f;

#define FOR(i, b, e) for(int i = (b); i < (e); i++)
#define TRAV(x, a) for(auto &x: (a))
#define SZ(x) ((int)(x).size())
#define PB push_back
#define X first
#define Y second

constexpr int N = 1e5 + 5;

int a[N], b[N];
int n, k;
int wart[N];
ll limit[N];
int rozm;

void dodaj(int val, ll akt) {
	wart[rozm] = val, limit[rozm] = akt;
	rozm++;
	FOR(i, 0, rozm) limit[i] = min(limit[i], akt);
}

ii maxim() {
	int zysk = -INF * 2, ind = -1;
	FOR(i, 0, rozm) if(min(1ll * wart[i], limit[i]) > zysk) zysk = min(1ll * wart[i], limit[i]), ind = i;
	if(ind == -1) return {zysk, ind};
	FOR(i, 0, ind) limit[i] = min(limit[i], limit[ind] - zysk);
	FOR(i, ind, rozm) limit[i] -= zysk;
	wart[ind] = -INF * 2;
	return {zysk, ind};
}

int f(ll bound) {
	ll akt = 0;
	int ans = 0;
	rozm = 0;
	FOR(i, 0, n) {
		akt += b[i];
		dodaj(b[i] - a[i], akt);
		while(akt > bound) {
			int daj = maxim().X;
			if(daj <= 0) return INF;
			ans++;
			akt -= daj;
		}
		akt = max(akt, 0ll);
	}
	return ans;
}

vector<int> gen(ll bound) {
	vi vec(n);
	ll akt = 0;
	int ans = 0;
	rozm = 0;
	FOR(i, 0, n) {
		akt += b[i];
		dodaj(b[i] - a[i], akt);
		while(akt > bound) {
			ii daj = maxim();
			vec[daj.Y] = 1;
			ans++;
			akt -= daj.X;
		}
		akt = max(akt, 0ll);
	}
	return vec;
}

bool check(ll bound) {
	if(f(bound) > k) return false;
	FOR(i, 0, n) swap(a[i], b[i]);
	int ile = f(bound);
	FOR(i, 0, n) swap(a[i], b[i]);
	return ile <= n - k;
}

void solve() {
	cin >> n >> k;
	FOR(i, 0, n) cin >> a[i];
	FOR(i, 0, n) cin >> b[i];
	ll l = -1, r = 1ll * n * INF, mid;
	while(l + 1 < r) {
		mid = (l + r) / 2;
		if(check(mid)) r = mid;
		else l = mid;
	}

	vector<int> v1 = gen(r);
	FOR(i, 0, n) swap(a[i], b[i]);
	reverse(a, a + n), reverse(b, b + n);
	vector<int> v2 = gen(r);
	reverse(v2.begin(), v2.end());
	for(auto &x: v2) x ^= 1;
	reverse(a, a + n), reverse(b, b + n);
	FOR(i, 0, n) swap(a[i], b[i]);

	int diff = 0, rem = k;
	FOR(i, 0, n) diff += v1[i] != v2[i], rem -= v1[i] == 1;
	vector<vector<ll>> dp(diff + 1, vector<ll>(rem + 1, 1ll * INF * INF));
	vector<vi> back(diff + 1, vector<int>(rem + 1, INF));
	dp[0][0] = 0;
	int akt = 0;
	FOR(i, 0, n) {
		if(v1[i] == v2[i]) {
			int val = (v1[i] ? a[i] : b[i]);
			TRAV(x, dp[akt]) x += val;
		}
		else {
			akt++;
			dp[akt][0] = dp[akt - 1][0] + b[i], back[akt][0] = 0;
			FOR(j, 1, rem + 1) {
				ll v5 = dp[akt - 1][j] + b[i];
				ll v6 = dp[akt - 1][j - 1] + a[i];
				if(v5 < v6) dp[akt][j] = v5, back[akt][j] = j;
				else dp[akt][j] = v6, back[akt][j] = j - 1;
			}
		}
		TRAV(x, dp[akt]) if(x > r) x = 1ll * INF * INF;
	}
	vi choose;
	int gdz = rem;
	for(int i = diff; i > 0; i--) {
		choose.PB(back[i][gdz] != gdz);
		gdz = back[i][gdz];
	}
	vi v3(n);
	FOR(i, 0, n) {
		if(v1[i] == v2[i]) v3[i] = v1[i];
		else v3[i] = choose.back(), choose.pop_back();
	}
	cout << r << '\n';
	FOR(i, 0, n) cout << (v3[i] ? 'A' : 'B');
	cout << '\n';
}

int main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	// int tt; cin >> tt;
	// FOR(te, 0, tt) {
	// 	// cout << "Case #" << te + 1 << ": ";
	// 	solve();
	// }
	solve();
	return 0;
}