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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <bits/stdc++.h>
#define FOR(i, n) for(int i = 0; i < (n); ++i)
#define REP(i, a, b) for(int i = (a); i < (b); ++i)
#define TRAV(i, a) for(auto & i : (a))
#define SZ(x) ((int)(x).size())
#define X first
#define Y second
#define PR std::pair
#define MP std::make_pair
typedef long long ll;
typedef std::pair<int, int> PII;
typedef std::vector<int> VI;

class MinTree{
public:
	static constexpr ll INF = 1'000'000'666ll * 1'000'000'000ll;
	static constexpr int N = 100'005;

	int n;  // array size
	ll t[2 * N];
	ll d[N]; 
	int h;

	void build(int n_){
		n = n_;
		std::fill(t, t + (2*N), 0);
		std::fill(d, d + N, 0);
		h = sizeof(int) * 8 - __builtin_clz(n);
	}

	void apply(int p, ll value) {
		t[p] += value;
		if (p < n) d[p] += value;
	}

	void jazda(int p) {
		while (p > 1) p >>= 1, t[p] = std::min(t[p<<1], t[p<<1|1]) + d[p];
	}

	void push(int p) {
		for (int s = h; s > 0; --s) {
			int i = p >> s;
			if (d[i] != 0) {
				apply(i<<1, d[i]);
				apply(i<<1|1, d[i]);
				d[i] = 0;
			}
		}
	}

	void add(int l, int r, ll value) {
		l += n, r += n+1;
		int l0 = l, r0 = r;
		for (; l < r; l >>= 1, r >>= 1) {
			if (l&1) apply(l++, value);
			if (r&1) apply(--r, value);
		}
		jazda(l0);
		jazda(r0 - 1);
	}

	ll query(int l, int r) {
		l += n, r += n+1;
		push(l);
		push(r - 1);
		ll res = INF;
		for (; l < r; l >>= 1, r >>= 1) {
			if (l&1) res = std::min(res, t[l++]);
			if (r&1) res = std::min(t[--r], res);
		}
		return res;
	}
};


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

	int n, k;
	std::cin >> n >> k;
	VI A(n+1), B(n+1);
	REP(i, 1, n+1) std::cin >> A[i];
	REP(i, 1, n+1) std::cin >> B[i];

	std::vector<PR<PII, int>> C(n+1);
	int sum = 0;
	REP(i, 1, n+1){
		C[i] = MP(MP(std::min(A[i], B[i]), std::max(A[i], B[i]) - std::min(A[i], B[i])), A[i] < B[i] ? 1 : 0);
		sum += C[i].Y;
	}

	std::vector<bool> change(n+1);
	std::vector<bool> zost(n+1);
	int want = (sum < k ? 1 : 0);
	int need = std::abs(sum - k);
	int all_change = 0;
	REP(i, 1, n+1) if(C[i].Y != want){
		change[i] = true;
		all_change++;
		C[i].X.X = std::max(A[i], B[i]);
	}

	MinTree min;
	auto can = [&](ll m){
		min.build(n+1);
		std::fill(zost.begin(), zost.end(), false);
		std::priority_queue<PII> que;
		PII cand = MP(-1, -1);
		ll have = 0;
		int oddalem = 0;

		REP(i, 1, n+1){
			have += C[i].X.X;
			have = std::max(have, 0ll);
			min.add(i, i, have);
			if(change[i]){
				que.push(MP(C[i].X.Y, i));
			}

			while(have > m){
				if(cand.Y != -1){
					cand.X = min.query(cand.Y, i);
				}

				PII normal = MP(-1, -1);
				while(SZ(que)){
					PII top = que.top();
					que.pop();
					ll mn = min.query(top.Y, i);
					if(mn <= top.X){
						int hehe = mn;
						if(cand.X < hehe){
							cand = MP(hehe, top.Y);
						}
					}else{
						normal = top;
						break;
					}
				}

				if(normal.Y == -1 && cand.Y == -1) return false;
				oddalem++;
				if(all_change - oddalem < need) return false;

				if(normal.X > cand.X){
					zost[normal.Y] = true;
					min.add(normal.Y, i, -normal.X);
					have -= normal.X;
				}else{
					zost[cand.Y] = true;
					min.add(cand.Y, i, -cand.X);
					have -= cand.X;
					cand = MP(-1, -1);
					if(normal.Y != -1) que.push(normal);
				}
			}
		}

		return all_change - oddalem >= need;
	};

	ll left = 0;
	ll right = 1'000'000'000ll * 100'000ll + 5ll;
	while(left < right){
		ll mid = (left+right)/2;
		// std::cerr << mid << std::endl;
		if(can(mid)){
			right = mid;
		}else{
			left = mid+1;
		}
	}

	can(left);
	std::cout << left << "\n";

	REP(i, 1, n+1){
		if(need && (change[i] ^ zost[i])){
			need--;
			C[i].Y ^= 1;
		}

		if(C[i].Y){
			std::cout << 'A';
		}else{
			std::cout << 'B';
		}
	}

	std::cout << "\n";

	return 0;
}