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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define rep(i, a, b) for (int i = a; i <= b; i++)
#define per(i, a, b) for (int i = b; a <= i; i--)
#define cat(x) cerr << #x << " = " << x << '\n';
using ll = long long;
using namespace std;
using namespace __gnu_pbds;

struct Set {
	int t = 0;
	tree<pair<ll, int>, null_type, less<pair<ll, int>>, rb_tree_tag, tree_order_statistics_node_update> s;
	ll sum = 0;

	int size() {
		return int(s.size());
	}

	bool empty() {
		return s.empty();
	}

	ll max_element() {
		return (--s.end())->first;
	}

	ll min_element() {
		return s.begin()->first;
	}

	ll less_than(ll x) {
		return s.order_of_key({x, 0});
	}

	void add(ll x) {
		sum += x;
		s.insert({x, t++});
	}

	void remove(ll x) {
		sum -= x;
		s.erase(s.lower_bound(make_pair(x, 0)));
	}

	void clear() {
		sum = 0;
		s.clear();
	}
} setL, setR;

const int N = 1 << 17;

int n, k;
int a[N], b[N], c[N], res[N];

int L, R;
ll H;

bool solve(ll x) {
	L = 0;
	R = 0;
	H = 0;
	setL.clear();
	setR.clear();

	rep(i, 1, n) {
		int d = a[i] - b[i];

		if (d >= 0) {
			c[i] = R + 1 + setR.less_than(d);
			setR.add(d);
			H += b[i];
		}
		else {
			d *= -1;
			c[i] = L + 1 - setL.less_than(d);
			setL.add(d);

			L += 1;
			R += 1;
			H -= d;
			H += b[i];
		}

		if (H < 0) {
			auto lift = [&](Set &a, int &border, int coef) {
				ll sum = 0;
				while (!a.empty() && a.min_element() + sum + H <= 0) {
					border += coef;
					sum += a.min_element();
					a.remove(a.min_element());
				}
				if (!a.empty()) {
					ll x = a.min_element();
					a.remove(x);
					a.add(x + sum + H);
				}
			};

			lift(setL, L, -1);
			lift(setR, R, +1);
			H = 0;
		}

		if (H > x) {
			return false;
		}

		auto relax = [&](Set &a) {
			while (!a.empty() && a.sum + H > x) {
				a.remove(a.max_element());
			}
		};

		relax(setL);
		relax(setR);
	}

	return L - setL.size() <= k && k <= R + setR.size();
}

int main() {
	cin.tie(0)->sync_with_stdio(0);

	cin >> n >> k;
	rep(i, 1, n) cin >> a[i];
	rep(i, 1, n) cin >> b[i];

	ll l = 0, r = 1e14;
	while (l < r) {
		ll m = (l + r) / 2;
		if (solve(m))
			r = m;
		else
			l = m + 1;
	}

	solve(l);

	per(i, 1, n) {
		if (c[i] <= k) {
			res[i] = 0;
			k--;
		}
		else {
			res[i] = 1;
		}
	}

	assert(k == 0);

	ll best = 0;
	ll cur = 0;

	rep(i, 1, n) {
		cur = max(0ll, cur + (res[i] ? b[i] : a[i]));
		best = max(best, cur);
	}

	assert(best == l);

	cout << l << '\n';
	rep(i, 1, n)
		cout << (res[i] ? 'B' : 'A');
	return 0;
}