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
#include <bits/stdc++.h>
#include <math.h>
using namespace std;
#define endl "\n"
#define mp make_pair
#define st first
#define nd second
#define pii pair<int, int>
#define pb push_back
#define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0);
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define fwd(i, a, b) for (int i = (a); i < (b); ++i)
#define trav(a, x) for (auto &a : x)
#define all(c) (c).begin(), (c).end()
#define sz(X) (int)((X).size())
typedef long double ld;
typedef unsigned long long ull;
typedef long long ll;
// mt19937_64 gen(2137);uniform_int_distribution<int> distr(0, 1e9);auto my_rand = bind(distr, gen); // my_rand() zwraca teraz liczbe z przedzialu [a, b]
#ifdef LOCAL
ostream &operator<<(ostream &out, string str) {
   for (char c : str)
      out << c;
   return out;
}
template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; }
template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) {
   auto &[a, b, c] = p;
   return out << "(" << a << ", " << b << ", " << c << ")";
}
template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
   out << '{';
   for (auto it = a.begin(); it != a.end(); it = next(it))
      out << (it != a.begin() ? ", " : "") << *it;
   return out << '}';
}
void dump() { cerr << "\n"; }
template <class T, class... Ts> void dump(T a, Ts... x) {
   cerr << a << ", ";
   dump(x...);
}
#define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
#define debug(...) 42
#endif

#define int long long

int n, k;

const int inf = 1e18;
const pii emp = {-1, -1};
const vector<int> empv = {-1};

struct BlackBox {

   int sum = 0;
   set<pii> Q;

   void ins(pii p) {
      Q.insert(p);
      sum += p.st;
   }

   pii get() {
      if (sz(Q) == 0)
         return emp;
      auto itr = Q.end();
      auto res = *(--itr);
      sum -= res.st;
      Q.erase(itr);
      return res;
   }

   void add(pii p) { ins(p); }

   void mini(int p) {
      while (sum > p) {
         auto [v, i] = *Q.begin();
         Q.erase(Q.begin());
         sum -= v;
         int r = p - sum;
         if (r > 0)
            ins({r, i});
      }
   }
};

vector<int> get(vector<int> &A, vector<int> &B, int s) {
   int cur = 0;
   BlackBox BB;
   vector<int> res;
   rep(i, n) {
      cur += B[i];
      int zysk = B[i] - A[i];
      if (zysk > 0)
         BB.add({zysk, i});
      while (cur > s) {
         auto t = BB.get();
         if (t == emp)
            return empv;
         auto [v, i] = t;
         res.push_back(i);
         cur -= v;
      }
      cur = max(cur, 0ll);
      BB.mini(cur);
   }
   return res;
}

bool ok(vector<int> &A, vector<int> &B, int s) {
   auto AA = get(A, B, s);
   auto BB = get(B, A, s);
   if (AA == empv || BB == empv)
      return false;
   pii t = {sz(AA), n - sz(BB)};
   auto [p, q] = t;
   return k >= p && k <= q;
}

int solve(vector<int> A, vector<int> B) {
   int p = 0;
   int q = n * 1e9 + 22;
   while (p != q) {
      int s = (p + q) / 2;
      if (ok(A, B, s))
         q = s;
      else
         p = s + 1;
   }
   return p;
}

set<int> get_set(vector<int> AA, vector<int> SA, int target) {
   set<int> P(all(AA));

   for (int a : SA) {
      if (sz(P) == target)
         return P;
      P.insert(a);
   }
   assert(sz(P) == target);
   return P;
}
string get(set<int> pos, char a, char b) {
   string res;
   rep(i, n) if (pos.count(i)) res.push_back(a);
   else res.push_back(b);
   return res;
}

pair<int, string> final(vector<int> A, vector<int> B) {
   int s = solve(A, B);

   auto AA = get(A, B, s);
   auto BB = get(B, A, s);

   vector<int> SA, SB;
   rep(i, n) {
      if (A[i] >= B[i])
         SB.push_back(i);
      if (A[i] <= B[i])
         SA.push_back(i);
   }

   string res;

   if (sz(SA) >= k) {
      set<int> pos = get_set(AA, SA, k);
      res = get(pos, 'A', 'B');
   } else {
      set<int> pos = get_set(BB, SB, n - k);
      res = get(pos, 'B', 'A');
   }
   return {s, res};
}

int32_t main() {
   _upgrade;
   cin >> n >> k;
   vector<int> A(n), B(n);
   rep(i, n) cin >> A[i];
   rep(i, n) cin >> B[i];

   auto [s, S] = final(A, B);

   cout << s << endl;
   cout << S << endl;
}