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
#include<bits/stdc++.h>
using namespace std;
using LL=long long;
#define FOR(i,l,r) for(int i=(l);i<=(r);++i)
#define REP(i,n) FOR(i,0,(n)-1)
#define ssize(x) int(x.size())
template<class A,class B>auto&operator<<(ostream&o,pair<A,B>p){return o<<'('<<p.first<<", "<<p.second<<')';}
template<class T>auto operator<<(ostream&o,T x)->decltype(x.end(),o){o<<'{';int i=0;for(auto e:x)o<<(", ")+2*!i++<<e;return o<<'}';}
#ifdef DEBUG
#define debug(x...) cerr<<"["#x"]: ",[](auto...$){((cerr<<$<<"; "),...)<<'\n';}(x)
#else
#define debug(...) {}
#endif

struct Elem {
	LL best, left, right, sum;
	Elem() : best(-1), left(0), right(0), sum(0) {}
	Elem(LL x) : best(max(0ll, x)), left(x), right(x), sum(x) {}
};

Elem merge(Elem a, Elem b) {
	Elem c;
	c.best = max({a.best, b.best, a.right + b.left});
	c.left = max(a.left, a.sum + b.left);
	c.right = max(b.right, b.sum + a.right);
	c.sum = a.sum + b.sum;
	return c;
}

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

	const LL INF = (1ll << 60);
	int n, k;
	cin >> n >> k;
	LL finans = INF;
	vector<char> finansv(n);
	vector<LL> a(n), b(n);
	vector<LL> sa(n + 1), sb(n + 1);
	REP(i,n) cin >> a[i];
	REP(i,n) cin >> b[i];
	REP(i,n) sa[i + 1] = a[i];
	REP(i,n) sb[i + 1] = b[i];
	auto check = [&]() {
		REP(i,n) assert(finansv[i] == 'A' or finansv[i] == 'B');
		int counta = 0;
		REP(i,n) counta += (finansv[i] == 'A');
		assert(counta == k);
		LL score = 0;
		LL prev = 0;
		REP(i,n) {
			LL val = (finansv[i] == 'A' ? a[i] : b[i]);
			LL cur = val + max(prev, 0ll);
			score = max(score, cur);
			prev = cur;
		}
		assert(score == finans);
	};
	auto print = [&]() {
		cout << finans << '\n';
		#ifdef LOCAL
			debug(finansv);
		#else
			REP(i,n) cout << finansv[i];
			cout << '\n';
		#endif
		check();
	};
	assert((LL)(k + 1) * (n - k - 1) < 70'000'000);
	vector<vector<LL>> dp(k + 1, vector<LL> (n - k + 1));
	function<bool(LL,bool)> dasie = [&](LL ss, bool odtw) {
		dp[0][0] = 0;

		FOR(j,1,n-k) {
			dp[0][j] = max(0ll, dp[0][j - 1] + sb[j]);
			if (dp[0][j] > ss) dp[0][j] = INF;
		}

		FOR(i,1,k) {
			dp[i][0] = max(0ll, dp[i - 1][0] + sa[i]);
			if (dp[i][0] > ss) dp[i][0] = INF;

			FOR(j,1,n-k) {
				dp[i][j] = max(0ll, min(dp[i - 1][j] + sa[i + j], dp[i][j - 1] + sb[i + j]));
				if (dp[i][j] > ss) dp[i][j] = INF;
			}
		}
		if (!odtw) return (dp[k][n - k] <= ss);
		int x = k, y = n - k;
		REP(i,n) {
			if (y == 0 || (x > 0 && dp[x - 1][y] + sa[x + y] <= dp[x][y - 1] + sb[x + y])) {
				finansv[n - i - 1] = 'A';
				--x;
			}
			else {
				finansv[n - i - 1] = 'B';
				--y;
			}
		}
		return true;
	};
	auto ogrmin = [&]() {
		LL score = 0;
		LL prev = 0;
		REP(i,n) {
			LL val = min(a[i], b[i]);
			LL cur = val + max(prev, 0ll);
			score = max(score, cur);
			prev = cur;
		}
		return score;
	};
	auto ogrmax = [&]() {
		LL score = 0;
		LL prev = 0;
		REP(i,n) {
			LL val = max(a[i], b[i]);
			LL cur = val + max(prev, 0ll);
			score = max(score, cur);
			prev = cur;
		}
		return score;
	};
	LL bb = ogrmin(), ee = ogrmax(), ss;
	while (bb <= ee) {
		ss = (bb + ee) / 2;
		debug(bb, ee, ss);
		if (dasie(ss, false)) {
			finans = min(finans, ss);
			ee = ss - 1;
		}
		else {
			bb = ss + 1;
		}
		debug(finans);
	}
	dasie(finans, true);
	print();
}