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
#include<bits/stdc++.h>
using namespace std;
using lld = long long;

const lld INF = 1e18;

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

	int n,k;
	cin >> n >> k;

	vector<int> a(n), b(n);
	for(int& i : a) cin >> i;
	for(int& i : b) cin >> i;

	lld value = INF;
	string result;

	string p(n,'B');
	fill(p.begin(),p.begin()+k,'A');
	do {
		lld x = 0;
		lld mn = 0, sum = 0;
		for(int i=0;i<n;i++) {
			sum += (p[i] == 'A' ? a[i] : b[i]);
			mn = min(mn, sum);
			x = max(x, sum-mn);
		}

		if(x < value) {
			value = x;
			result = p;
		}
	} while(next_permutation(p.begin(), p.end()));

	cout << value << "\n" << result << "\n";

	return 0;
}