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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include <bits/stdc++.h>
using namespace std;

#define FOR(i, n) for (int i = 0; i < int(n); ++i)
#define FO(i, a, b) for (int i = (a); i < int(b); ++i)
#define OF(i, a, b) for (int i = (b)-1; i >= int(a); --i)

#define MIN(a, b) ((a) < (b) ? (a) : (b))
#define MAX(a, b) ((b) < (a) ? (a) : (b))

#define REMIN(a, b) ((a) = min(a, b))
#define REMAX(a, b) ((a) = max(a, b))

#define ALL(c) (c).begin(), (c).end()

#define SQR(x) ((x) * (x))

//

// 1-based indices
class Splay {

  struct Node {
    //
    // EDIT HERE:
    long long y = 0;
    long long yAdd = 0;
    //
    int num = 1;
    //

    int c[2] = {0, 0}; // children
    int p = 0;         // parent
  };

  vector<Node> v;

public:
  void propagate(int x) {
    if (!x)
      return;

    v[x].y += v[x].yAdd;

    if (v[x].c[0])
      v[v[x].c[0]].yAdd += v[x].yAdd;

    if (v[x].c[1])
      v[v[x].c[1]].yAdd += v[x].yAdd;

    v[x].yAdd = 0;
  }

  void aggregate(int x) {
    v[x].num = 1;
    FOR(ch, 2) if (v[x].c[ch]) v[x].num += v[v[x].c[ch]].num;
  }

  void aggregate_to_root(int x) {
    while (x) {
      aggregate(x);
      x = v[x].p;
    }
  }

  Splay() : v(1) {}
  Splay(int size) : v(size + 1) {}

  int is_ith(int x) const { return v[v[x].p].c[1] == x; }

  void rotate(int x) {
    // cerr << "rotate " << x << endl;

    int ith = is_ith(x);
    int p = v[x].p;       // parent
    int g = v[p].p;       // grand-parent
    int i = v[x].c[!ith]; // inner child

    // propagate(g);
    propagate(p);
    propagate(x);
    // propagate(i);

    v[x].p = g;
    if (g)
      v[g].c[is_ith(p)] = x;

    v[x].c[!ith] = p;
    v[p].p = x;

    v[p].c[ith] = i;
    v[i].p = p;

    aggregate(p);
    aggregate(x);
  }

  void splay_to_root(int x) {
    // cerr << "splay_to_root " << x << endl;
    for (;;) {
      int p = v[x].p; // parent
      if (!p)
        break;

      int g = v[p].p;
      if (!g) {
        rotate(x);
        break;
      }

      if (is_ith(x) == is_ith(p)) {
        rotate(p);
        rotate(x);
      } else {
        rotate(x);
        rotate(x);
      }
    }
    propagate(x);
    aggregate(x);
  }

  // assumes:
  //   each element in x <= each element in y
  int merge(int x, int y) {
    splay_to_root(x);

    while (v[y].c[0])
      y = v[y].c[0];
    splay_to_root(y);

    v[x].p = y;
    v[y].c[0] = x;

    return y;
  }

  // split tree into 2 trees of elements (_ < x) and (_ >= x)
  pair<int, int> split(int x) {
    splay_to_root(x);
    int c0 = v[x].c[0];
    if (c0) {
      v[x].c[0] = 0;
      v[c0].p = 0;
    }
    return {c0, x};
  }

  // find first element that satisfies `pred(idx)`,
  // assuming `pred` is monotonic: first `false`, then `true`
  template <class PRED> int lower_bound(int x, const PRED &pred) {
    splay_to_root(x);
    // cerr << "lower_bound " << x << endl;
    int cand = 0;
    for (;;) {
      auto r = pred(x);
      if (r) {
        // cerr << "  cand " << x << endl;
        cand = x;
      }

      int next = v[x].c[!r];
      // cerr << "  next " << next << endl;
      if (!next) {
        splay_to_root(x);
        break;
      }

      x = next;
    }
    return cand;
  }

  // repeat for all x, while fun(x) == true
  template <class FUN> void each_descendant(int x, const FUN &fun) {
    if (!x)
      return;

    each_descendant(v[x].c[0], fun);
    if (!fun(x))
      return;
    each_descendant(v[x].c[1], fun);
  }

  template <class FUN> void each(int x, const FUN &fun) {
    splay_to_root(x);
    each_descendant(x, fun);
  }

  // this can't splay, because can be called from within lambdas
  int next(int x, bool forward = true) {
    propagate(x);
    if (v[x].c[forward]) {
      x = v[x].c[forward];
      propagate(x);
      while (v[x].c[!forward]) {
        x = v[x].c[!forward];
        propagate(x);
      }
      return x;
    } else
      for (;;) {
        int p = v[x].p;
        if (!p)
          return 0;

        propagate(p);

        if (is_ith(x) != forward)
          return p;
        x = p;
      }
  }

  int prev(int x) { return next(x, false); }

  int first(int x) {
    splay_to_root(x);
    while (v[x].c[0])
      x = v[x].c[0];
    splay_to_root(x);
    return x;
  }

  int last(int x) {
    splay_to_root(x);
    while (v[x].c[1])
      x = v[x].c[1];
    splay_to_root(x);
    return x;
  }

  // helpers
  Node &operator[](int i) { return v[i]; }
};

//

const int N = 100'000 + 9;

int n, k;
int input[N][2];

int res[N][3];

// long long dp[N][2];
// bitset<N> from[N / 2];

bool test(long long limit) {
  // cerr << "test " << limit << endl;

  Splay splay(N);
  int nextIndex = 1;
  int anyIndex = 0;
  long long level = 0;

  splay[nextIndex].y = 0;
  anyIndex = nextIndex;
  ++nextIndex;

  int erasedFront = 0;

  FOR(i, n) {
    // find intersection (element to duplicate)
    int x = anyIndex;
    int foundX = 0;
    splay.splay_to_root(x);
    while (x) {
      splay.propagate(x);
      auto y0 = splay[x].y;
      REMAX(y0, level);

      auto prev = splay.prev(x);
      if (prev == 0) { // first element too small
        x = splay[x].c[1];
        continue;
      }
      // splay.splay_to_root(prev);
      auto y1 = splay[prev].y;
      REMAX(y1, level);

      auto cand0 = y0 + input[i][1];
      auto cand1 = y1 + input[i][0];

      // splay.splay_to_root(x);

      if (cand1 <= cand0) {
        foundX = x;
        x = splay[x].c[0];
      } else {
        x = splay[x].c[1];
      }
    }

    if (!anyIndex)
      return true;

    int dup = splay.last(anyIndex);
    if (foundX)
      dup = splay.prev(foundX);

    // cerr << "foundX y==" << splay[foundX].y << endl;

    assert(dup);

    splay.splay_to_root(dup);
    REMAX(splay[dup].y, level);
    auto dupY = splay[dup].y;

    auto dupNext = splay.next(dup);

    if (dupNext) {
      assert(splay[dupNext].c[0] == 0);
      splay[dupNext].c[0] = nextIndex;
      splay[nextIndex].p = dupNext;
      splay.aggregate_to_root(dupNext);
      splay.splay_to_root(nextIndex);
    } else {
      assert(splay[dup].c[1] == 0);
      splay[dup].c[1] = nextIndex;
      splay[nextIndex].p = dup;
      splay.aggregate_to_root(dup);
      splay.splay_to_root(nextIndex);
    }
    // splay[nextIndex].y = max(0LL, dupY);
    splay[nextIndex].y = dupY;

    auto yMod = input[i][0] - input[i][1];
    splay[nextIndex].y += yMod;
    if (splay[nextIndex].c[1])
      splay[splay[nextIndex].c[1]].yAdd += yMod;

    splay[nextIndex].yAdd += input[i][1];
    ++nextIndex;

    level = max(0LL, level + min(input[i][0], input[i][1]));

    bool dup_was_before = false;
    bool dup_was_after = false;

    // remove too big elements from beginning and end

    for (;;) {
      if (anyIndex == 0)
        break;

      splay.splay_to_root(anyIndex);
      anyIndex = splay.first(anyIndex);
      splay.splay_to_root(anyIndex);
      if (max(level, splay[anyIndex].y) < limit)
        break;

      ++erasedFront;
      if (anyIndex == dup)
        dup_was_before = true;

      anyIndex = splay[anyIndex].c[1];
      if (anyIndex)
        splay[anyIndex].p = 0;
    }

    for (;;) {
      if (anyIndex == 0)
        break;

      splay.splay_to_root(anyIndex);
      anyIndex = splay.last(anyIndex);
      splay.splay_to_root(anyIndex);
      if (max(level, splay[anyIndex].y) < limit)
        break;

      if (anyIndex == dup)
        dup_was_after = true;

      anyIndex = splay[anyIndex].c[0];
      if (anyIndex)
        splay[anyIndex].p = 0;
    }

    splay.splay_to_root(anyIndex);

    res[i][0] = erasedFront;
    res[i][2] = erasedFront + splay[anyIndex].num;

    if (dup_was_before)
      res[i][1] = res[i][0];
    else if (dup_was_after)
      res[i][1] = res[i][2];
    else {
      splay.splay_to_root(dup);
      res[i][1] =
          erasedFront + (splay[dup].c[0] ? splay[splay[dup].c[0]].num : 0) + 1;
    }

    assert(res[i][0] <= res[i][1]);
    assert(res[i][1] <= res[i][2]);

    // cerr << "res[" << i << "] == " << res[i][0] << " " << res[i][1] << " "
    //      << res[i][2] << endl;

    // // PRINT
    // {
    //   splay.splay_to_root(anyIndex);
    //   cerr << erasedFront << " erased, then (" << splay[anyIndex].num << "):
    //   "; int xx = anyIndex; if (xx) {
    //     xx = splay.first(xx);
    //     while (xx) {
    //       splay.splay_to_root(xx);
    //       cerr << splay[xx].y << " ";
    //       xx = splay.next(xx);
    //     }
    //   }
    //   cerr << ", level:" << level << endl;
    // }
  }

  if (level >= limit)
    return true;

  splay.splay_to_root(anyIndex);
  return k < erasedFront || erasedFront + splay[anyIndex].num <= k;
}

int main() {
  ios_base::sync_with_stdio(0);

  cin >> n >> k;

  bool swapped = false;
  if (k >= n / 2) {
    swapped = true;
    k = n - k;
  }

  FOR(j, 2) FOR(i, n) cin >> input[i][j ^ swapped];

  // bool testResult = test(8);
  // cerr << "test result " << testResult << endl;
  // return 0;

  long long a = 0;
  long long b = 1LL * N * (int)1e9;

  while (a + 1 < b) {
    auto mid = (a + b) / 2;

    if (test(mid))
      a = mid;
    else
      b = mid;
  }

  test(a + 1);

  cout << a << endl;

  vector<int> result;
  OF(x, 0, n) {
    if (k >= res[x][1]) {
      --k;
      result.push_back(0);
    } else {
      result.push_back(1);
    }
  }

  OF(i, 0, n) { cout << ((result[i] ^ swapped) ? 'B' : 'A'); }
  cout << endl;

  assert(k == 0);

  return 0;
}