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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include<bits/stdc++.h>
using namespace std;

#define FOR(i, a, n) for(decltype(n) i = (a), i##__ = (n); i <= i##__; i++)
#define REP(i, n) FOR(i, 0, (n)-1)
#define FORD(i, a, n) for(decltype(a) i = (a), i##__ = (n); i >= i##__; i--)
#define REPD(i, n) FORD(i, (n)-1, 0)
#define V vector
#define ST first
#define ND second
#define EB emplace_back
#define RS resize
#define SZ(x) int(x.size())
#define ALL(x) x.begin(), x.end()
#define OS ostream

template<class A, class B> OS& operator<<(OS &os, pair<A, B> p) {
	return os << "(" << p.ST << ", " << p.ND << ")";
}
template<class A> OS& operator<<(OS &os, V<A> v) {
	os << "{ ";
	for(auto e : v) os << e << " ";
	return os << "}";
}
template<class A> OS& operator<<(OS &os, V<V<A>> v) {
	os << "[\n";
	for(auto e : v) os << "  " << e << "\n";
	return os << "]";
}

#ifdef DEBUG
# define D(a...) a
# define _D(a...)
#else
# define D(a...)
# define _D(a...) a
#endif
# define I(a...) #a << ": " << a << "\n"
# define J(a...) #a << ": " << a << "  "

using VI   = V<int>;
using VVI  = V<VI>;
using VVVI = V<VVI>;
using II   = pair<int, int>;
using VII  = V<II>;
using VVII = V<VII>;
using L    = long long;
using VL   = V<L>;
using VVL  = V<VL>;
using LI   = pair<L, int>;
using VB   = V<bool>;
using VVB  = V<VB>;

constexpr int mod = int(1e9+7);

struct node {
	L sum;
	L max_pref;
	L max_suf;
	L max_sum;
	node (L x = 0): 
		sum(x), 
		max_pref(max(x, 0ll)), 
		max_suf(max(x, 0ll)), 
		max_sum(max(x, 0ll)) 
		{}
};

node operator+(node &a, node &b) {
	node c;
	c.sum = a.sum + b.sum;
	c.max_pref = max(a.max_pref, a.sum + b.max_pref);
	c.max_suf = max(b.max_suf, a.max_suf + b.sum);
	c.max_sum = max(a.max_sum, max(b.max_sum, a.max_suf + b.max_pref));
	return c;
}

struct Tree {
	vector<node> tr;
	int sz;
	Tree(VI &b) {
		int n = SZ(b);
		sz = 1;
		while (sz < n)
			sz *= 2;
		tr.RS(sz * 2);
		for (int i = 0; i < n; i++)
			tr[i + sz] = node(b[i]);
		for (int i = sz - 1; i > 0; i--)
			tr[i] = tr[i * 2] + tr[i * 2 + 1];
	}
	void add(int where, int what) {
		where += sz;
		tr[where] = node(what);
		while (where >>= 1) {
			tr[where] = tr[where * 2] + tr[where * 2 + 1];	
		}
	}
	L get() {
		return tr[1].max_sum;
	}
};

int n, k;
VI a, b;
VI best;
L answer = L(1e18);
void backtrack(Tree &t, VI& elts, int id = 0, int bnd = 0) {
	if (id == k) {
		L ans = t.get();
		D(cerr << "Looking at " << I(elts) << ", got " << ans << "\n");
		if (ans < answer) {
			best = elts;
			answer = ans;
		}
	}
	else for (int j = bnd; j < n; j++) {
		elts.EB(j);
		t.add(j, a[j]);
		backtrack(t, elts, id + 1, j + 1);
		elts.pop_back();
		t.add(j, b[j]);
	}
}

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

	cin >> n >> k;
	a.RS(n); b.RS(n);
	REP(i, n)
		cin >> a[i];
	REP(i, n)
		cin >> b[i];
	VI elts;
	Tree t(b);
	backtrack(t, elts);
	cout << answer << "\n";
	string s(n, 'B');
	for (int i : best)
		s[i] = 'A';
	cout << s << "\n";
}