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
#include <stdio.h>
#include <stdlib.h>

#define INF 1000000000000000

#define min(a, b) ((a) < (b) ? (a) : (b))
#define max(a, b) ((a) > (b) ? (a) : (b))

int a[32];
int b[32];

int arr[32];
long long dp[33];

long long do_dp(int n)
{
	int i;
	long long r = 0;

	for (i = 0; i < n; i++) {
		dp[i+1] = max(0, arr[i] + dp[i]);
		r = max(r, dp[i+1]);
	}
	
	return r;
}

int main(void)
{
	int n, k;
	long long m, mm = 0;
	long long best = INF;
	long long r;
	int i;

	scanf("%d%d", &n, &k);
	for (i = 0; i < n; i++)
		scanf("%d", &a[i]);
	for (i = 0; i < n; i++)
		scanf("%d", &b[i]);

	for (m = 0; m < 1ll << n; m++) {
		if (__builtin_popcountll(m) != k)
			continue;
		for (i = 0; i < n; i++)
			arr[i] = m & (1ll << i) ? a[i] : b[i];
		r = do_dp(n);
		if (r < best) {
			best = r;
			mm = m;
		}
	}

	printf("%lld\n", best);
	for (i = 0; i < n; i++)
		printf("%c", mm & (1ll << i) ? 'A' : 'B');
	printf("\n");

	return 0;
}