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
#include <bits/stdc++.h>
using namespace std;

const int64_t MAXVAL = 1e14;
const int64_t INFTY = 1e18;

vector<bool> solution;

bool solve(const vector<int>& a, const vector<int>& b, int k, int64_t limit, bool recover=false) {
  int n = a.size();
  vector<vector<int64_t>> dp(n + 1, vector<int64_t>(k + 1, 5 * MAXVAL));
  dp[0][0] = 0;
  for (int i = 0; i < n; ++i) {
    dp[i + 1][0] = max((int64_t)0, dp[i][0] + b[i]);
    for (int j = 1; j <= k; ++j) {
      dp[i + 1][j] = max((int64_t)0, min(dp[i][j - 1] + a[i], dp[i][j] + b[i]));
    }
    for (int j = 0; j <= k; ++j)
      if (dp[i + 1][j] > limit)
        dp[i + 1][j] = 5 * MAXVAL;
  }
  if (recover) {
    assert(dp[n][k] <= limit);
    solution.resize(n);
    while (n > 0) {
      if (dp[n][k] == max((int64_t)0, dp[n - 1][k] + b[n - 1])) {
        solution[n - 1] = false;
      } else {
        solution[n - 1] = true;
        --k;
      }
      --n;
    }
    return true;
  }
  return dp[n][k] <= limit;
}

int main() {
  ios_base::sync_with_stdio(false);
  int n, k;
  cin >> n >> k;
  vector<int> a(n), b(n);
  for (int i = 0; i < n; ++i)
    cin >> a[i];
  for (int i = 0; i < n; ++i)
    cin >> b[i];
  int64_t l = 0, r = MAXVAL;
  while (l < r) {
    int64_t m = (l + r) / 2;
    if (solve(a, b, k, m))
      r = m;
    else
      l = m + 1;
  }
  cout << l << endl;
  solve(a, b, k, l, true);
  for (int i = 0; i < n; ++i)
    cout << (solution[i] ? 'A' : 'B');
  cout << endl;
}