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
//Autor: Bartłomiej Czarkowski
#include <bits/stdc++.h>
#pragma GCC optimize "O3"

using namespace std;

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
const int N = 1e5 + 7;
const long long INF = 1e18;
// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
int n, k, flip;
long long a[N];
long long b[N];

void flp() {
	k = n - k;
	for (int i = 1; i <= n; ++i) {
		swap(a[i], b[i]);
	}
}

void pr_in() {
	printf("%d %d\n", n, k);
	for (int i = 1; i <= n; ++i) {
		printf("%lld ", a[i]);
	}
	printf("\n");
	for (int i = 1; i <= n; ++i) {
		printf("%lld ", b[i]);
	}
	printf("\n");
}

bool check(vector<vector<long long>> &dp, long long tr) {
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= k; ++j) {
			dp[i][j] = INF;
		}
	}
	dp[0][0] = 0;
	for (int i = 1; i <= n; ++i) {
		dp[i][0] = max(dp[i - 1][0] + b[i], b[i]);
		if (dp[i][0] >= tr) {
			dp[i][0] = INF;
		}
		for (int j = 1; j <= k; ++j) {
			dp[i][j] = min(max(dp[i - 1][j] + b[i], b[i]), max(dp[i - 1][j - 1] + a[i], a[i]));
			if (dp[i][j] >= tr) {
				dp[i][j] = INF;
			}
		}
	}
	return dp[n][k] < tr;
}

void wypisz(vector<vector<long long>> &dp, vector<vector<bool>> &kto, long long tr) {
	for (int i = 0; i <= n; ++i) {
		for (int j = 0; j <= k; ++j) {
			dp[i][j] = INF;
		}
	}
	dp[0][0] = 0;
	for (int i = 1; i <= n; ++i) {
		dp[i][0] = max(dp[i - 1][0] + b[i], b[i]);
		kto[i][0] = 0;
		if (dp[i][0] > tr) {
			dp[i][0] = INF;
		}
		for (int j = 1; j <= k; ++j) {
			dp[i][j] = min(max(dp[i - 1][j] + b[i], b[i]), max(dp[i - 1][j - 1] + a[i], a[i]));
			if (max(dp[i - 1][j] + b[i], b[i]) < max(dp[i - 1][j - 1] + a[i], a[i])) {
				dp[i][j] = max(dp[i - 1][j] + b[i], b[i]);
				kto[i][j] = 0;
			}
			else {
				dp[i][j] = max(dp[i - 1][j - 1] + a[i], a[i]);
				kto[i][j] = 1;
			}
			if (dp[i][j] > tr) {
				dp[i][j] = INF;
			}
		}
	}
	vector<char> odp;
	int x = k;
	for (int i = n; i; --i) {
		if (kto[i][x]) {
			--x;
			odp.push_back('A');
		}
		else {
			odp.push_back('B');
		}
	}
	reverse(odp.begin(), odp.end());
	printf("%lld\n", tr);
	for (char i : odp) {
		printf("%c", 'A' + ((i - 'A') ^ flip));
	}
	printf("\n");
}

int main() {
	scanf("%d%d", &n, &k);
	long long bp = 0, bk = 0, bs;
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &a[i]);
	}
	for (int i = 1; i <= n; ++i) {
		scanf("%lld", &b[i]);
		bp += min(a[i], b[i]);
		bk += max({a[i], b[i], 0ll});
	}
	//~ pr_in();
	bp = max(bp, 0ll);
	++bk;
	if (k > n - k) {
		flip = 1;
		flp();
	}
	vector<vector<long long>> dp(n + 1, vector<long long>(k + 2, 0));
	vector<vector<bool>> kto(n + 1, vector<bool>(k + 2, 0));
	while (bk - bp > 1) {
		bs = (bp + bk) / 2;
		if (check(dp, bs)) {
			bk = bs;
		}
		else {
			bp = bs;
		}
	}
	wypisz(dp, kto, bp);
	return 0;
}