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
#include "bits/stdc++.h" // Ignacy Boehlke
using namespace std;     // XIII LO Szczecin
template<class A, class B> ostream& operator << (ostream& os, const pair<A, B>& p) {return os << '{' << p.first << ", " << p.second << '}';}
template<class T> auto operator << (ostream& os, const T& v) -> decltype(v.begin(), os) {os << '{';for (auto i : v) os << i << ", ";return os << '}';}
#ifdef DEBUG
#define D(x...) x
#else
#define D(x...)
#endif
#define LN(x) D(cerr << #x << ": " << x << ' ')
#define LOG(x) D(cerr << #x << ": " << x << '\n')
#define ssize(x) ((int)x.size())
#define FOR(a, b, c) for(int a = (b); a <= (c); ++a)
#define REP(a, b) FOR(a, 0, b - 1)
#define ALL(x) (x).begin(), (x).end()
#define fi first
#define se second
using ll = long long;

int n, k;
vector<int> a, b;
pair<ll, int> res = {LLONG_MAX, 0};
void bt(int i, int ck, ll mx, ll pref, int mn, int mask) {
    if (i == n) {
        res = min(res, {mx, mask});
        return;
    }
    if (ck)         bt(i + 1, ck - 1, max(mx, pref + a[i] - mn), pref + a[i], min(mn, a[i]), mask | (1 << i));
    if (n - i > ck) bt(i + 1, ck,     max(mx, pref + b[i] - mn), pref + b[i], min(mn, b[i]), mask);
}

int main() {
    scanf("%d%d", &n, &k);
    a.resize(n), b.resize(n);
    REP(i, n) scanf("%d", &a[i]);
    REP(i, n) scanf("%d", &b[i]);
    bt(0, k, 0, 0, 0, 0);

    printf("%lld\n", res.fi);
    REP(i, n) putchar((res.se & (1 << i)) ? 'A' : 'B');
    putchar('\n');
}