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
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
int n, k;
int t[100005][2];
string s, ans_string;
ll ans = 1e18;
int32_t main() {
    cin >> n >> k;
    for (int j = 0; j < 2; j++)
        for (int i = 1; i <= n; i++) 
            cin >> t[i][j];
    
    s = string(k, 'A') + string(n - k, 'B');
    do {
        ll sum = 0, best = 0, res = 0;
        for (int i = 1; i <= n; i++) {
            sum += t[i][s[i-1] - 'A'];
            res = max(res, sum - best);
            best = min(best, sum);
        }

        if (res < ans) {
            ans = res;
            ans_string = s;
        }
    } while (next_permutation(s.begin(), s.end()));

    cout << ans << "\n" << ans_string << "\n";
}