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
#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
#ifdef LOC
#include "debuglib.h"
#else
#define deb(...)
#define DBP(...)
#endif
using namespace std;
using   ll         = long long;
using   Vi         = vector<int>;
using   Pii        = pair<int, int>;
#define pb           push_back
#define mp           make_pair
#define x            first
#define y            second
#define rep(i, b, e) for (int i = (b); i < (e); i++)
#define each(a, x)   for (auto& a : (x))
#define all(x)       (x).begin(), (x).end()
#define sz(x)        int((x).size())

struct SegTree {
	struct Node {
		ll sum{0}, suf{0};
	};

	vector<Node> V;
	int len;

	SegTree(int n) {
		for (len = 1; len < n; len *= 2);
		V.resize(len*2);
	}

	void set(int i, ll a) {
		V[i += len] = { a, max(a, 0ll) };
		while (i /= 2) {
			auto &l = V[i*2], &r = V[i*2+1];
			V[i] = { l.sum + r.sum, max(l.suf + r.sum, r.suf) };
		}
	}

	ll getMaxSuf() const { return V[1].suf; }

	ll getNewMaxSuf(int i, ll a) {
		ll old = V[i+len].sum;
		set(i, a);
		ll ret = getMaxSuf();
		set(i, old);
		return ret;
	}
};

int findMinK(const Vi& elems1, const Vi& elems2, ll cap, string& out) {
	int n = sz(elems1), ret = 0;
	SegTree tree(n);
	priority_queue<pair<ll, int>> loose;
	priority_queue<int> tight;
	out.assign(n, 'B');

	rep(i, 0, n) {
		tree.set(i, elems1[i]);
		if (elems2[i] < elems1[i]) {
			loose.push({elems1[i]-elems2[i], i});
		}

		while (tree.getMaxSuf() > cap) {
			while (!loose.empty()) {
				auto t = loose.top();
				ll delta = tree.getMaxSuf() - tree.getNewMaxSuf(t.y, elems2[t.y]);
				if (delta == t.x) break;
				loose.pop();
				tight.push(t.y);
			}

			ll looseVal = (loose.empty() ? INT64_MAX : tree.getNewMaxSuf(loose.top().y, elems2[loose.top().y]));
			ll tightVal = (tight.empty() ? INT64_MAX : tree.getNewMaxSuf(tight.top(), elems2[tight.top()]));

			if (looseVal == INT64_MAX && tightVal == INT64_MAX) {
				return n+5;
			}

			int j;
			if (looseVal < tightVal) {
				j = loose.top().y;
				loose.pop();
			} else {
				j = tight.top();
				tight.pop();
			}

			tree.set(j, elems2[j]);
			out[j] = 'A';
			ret++;
		}
	}

	return ret;
}

int findMaxK(const Vi& elems1, const Vi& elems2, ll cap, string& out) {
	int ret = sz(elems1) - findMinK(elems2, elems1, cap, out);
	each(c, out) c = "AB"[c == 'A'];
	return ret;
}

bool isPossibleK(const Vi& elems1, const Vi& elems2, int k, ll cap) {
	string tmp;
	int minK = findMinK(elems2, elems1, cap, tmp);
	int maxK = findMaxK(elems2, elems1, cap, tmp);
	return minK <= k && k <= maxK;
}

bool fixSolution(const Vi& elems1, const Vi& elems2, int k, string& out) {
	int gotK = int(count(all(out), 'A'));
	rep(i, 0, sz(out)) {
		if (gotK < k && out[i] == 'B' && elems1[i] <= elems2[i]) {
			out[i] = 'A';
			gotK++;
		}
		if (gotK > k && out[i] == 'A' && elems1[i] >= elems2[i]) {
			out[i] = 'B';
			gotK--;
		}
	}
	return gotK == k;
}

void verifySolution(const Vi& A, const Vi& B, const string& out, ll ans, int n, int k) {
	assert(sz(A) == n && sz(B) == n && sz(out) == n);
	assert(int(count(all(out), 'A')) == k);
	assert(int(count(all(out), 'B')) == n-k);
	ll mx = 0, suf = 0;
	rep(i, 0, n) {
		ll val = (out[i] == 'A' ? A[i] : B[i]);
		suf = max(suf+val, 0ll);
		mx = max(mx, suf);
	}
	assert(mx == ans);
}

int main() {
	cin.sync_with_stdio(0); cin.tie(0);
	cout << fixed << setprecision(12);

	int n, k;
	cin >> n >> k;

	Vi elems1(n), elems2(n);
	each(e, elems1) cin >> e;
	each(e, elems2) cin >> e;

	ll b = 0, e = 1e15;

	while (b+1 < e) {
		ll m = (b+e) / 2;
		(isPossibleK(elems1, elems2, k, m-1) ? e : b) = m;
	}

	cout << b << '\n';

	string out;
	findMinK(elems2, elems1, b, out);

	if (!fixSolution(elems1, elems2, k, out)) {
		findMaxK(elems2, elems1, b, out);
		bool ok = fixSolution(elems1, elems2, k, out);
		assert(ok);
	}

#ifdef LOC
	verifySolution(elems1, elems2, out, b, n, k);
#endif
	cout << out << '\n';
	return 0;
}