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
// Szymon Rusiecki (09.12.2021)
#include <bits/stdc++.h>

const size_t mask_size = 1048576;

long long binom(long long n, long long k) {
	long long output = 1;
	for (int i = 1; i <= k; ++i) {
		output *= n - k + i;
		output /= i;
	}
	return output;
}
long long Kadane(long long *A, long long *B, std::bitset<mask_size> &bit_mask, int len) {
	long long output = LONG_LONG_MIN;
	long long maxi_value = 0;

	for (int i = 0; i < len; ++i) {
		if (bit_mask[i + 2] == 0) maxi_value += B[i];
		else maxi_value += A[i];

		if (output < maxi_value)
			output = maxi_value;
		
		if (maxi_value < 0)
			maxi_value = 0;
	}

	return output;
}

void add(std::bitset<mask_size> &bit_mask, int *index, int n, int k, int it) {
	if (bit_mask[index[it] + 1] == 0) {
		bit_mask[index[it]] = 0;
		++index[it];
		bit_mask[index[it]] = 1;
		return;
	}
	else {
		add(bit_mask, index, n, k, it - 1);
		bit_mask[index[it]] = 0;
		index[it] = index[it - 1] + 1;
		bit_mask[index[it]] = 1;
		return;
	}
}

int main(){
	std::ios_base::sync_with_stdio(false);
	std::cin.tie(nullptr);
	std::cout.tie(nullptr);

	long long n, k;
	std::cin >> n >> k;

	auto *A = new long long [n];
	auto *B = new long long [n];

	for (int i = 0; i < n; ++i)
		std::cin >> A[i];

	for (int i = 0; i < n; ++i)
		std::cin >> B[i];

	std::bitset<mask_size> bit_mask;
	std::bitset<mask_size> output;
	for (int i = 0; i < mask_size; ++i)
		bit_mask[i] = 0;
	for (int i = 2; i < k + 2; ++i)
		bit_mask[i] = 1;
	bit_mask[n + 2] = 1;
	bit_mask[0] = 1;

	int *index = new int [k + 1];
	for (int i = 1; i <= k; ++i)
		index[i] = i + 1;
	index[0] = 0;

	long long loop_size = binom(n, k);
	long long score = LONG_LONG_MAX;

	for (int i = 0; i < loop_size; ++i) {
		long long temp = Kadane(A, B, bit_mask, n);
			if (temp < score) {
				score = std::max(temp, 0LL);
				output = bit_mask;
			}
		add(bit_mask, index, n, k, k);
	}

	std::cout << score << '\n';
	for (int i = 2; i < n + 2; ++i)
		(output[i] == 1) ? std::cout << 'A' : std::cout << 'B';
	
	delete [] A;
	delete [] B;

	return 0; 
}