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
#include <cstdio>
#include <cassert>
#include <cinttypes>
#include <set>

#define MAXN 100200

typedef long long ll;

ll inputA[MAXN];
ll inputB[MAXN];
int N;
int K;
ll *A;
ll *B;
char Achar, Bchar;
char output[MAXN];

ll Add(ll x, ll y) {
  return (x + y > 0 ? x + y : 0);
}

typedef struct movement {
  ll val;
  int pos;
} movement;

bool operator< (const movement &A, const movement &B) {
  return (A.val < B.val) || (A.val == B.val && A.pos < B.pos);
}

bool CanAchieve(ll maxval) {
  ll optimalplay = 0;
  ll lastinset = 0;
  std::set<movement> current;
  int rejected = 0;
  for (int i = 0; i < N; ++i) {
    int newA = A[i];
    int newB = B[i];
    if (newB < newA) newA = newB;
    while (!current.empty() && Add(optimalplay + current.begin()->val, newA) == Add(optimalplay, newA)) {
      // Check if the first entry flattens by horizontal movement.
      assert(Add(optimalplay + current.begin()->val, newA) == 0);
      optimalplay += current.begin()->val; 
      current.erase(current.begin());
      if (current.empty()) {
        assert(optimalplay == lastinset);
      }
    }
    // OK, now horizontals are cleared. Let's check if the vertical disappears.
    if (Add(optimalplay, newA) == Add(optimalplay, newB)) {
      // OK, our thing disappears into the flatten as well. Toss it, we'll take newB for everything.
      if (!current.empty()) {
        movement first = *current.begin();
        current.erase(current.begin());
        ll newval = Add(optimalplay + first.val, newB) - Add(optimalplay, newB);
        assert(newval > 0LL);
        assert(newval <= first.val);
        first.val = newval;
        current.insert(first);
        assert(current.begin()->pos == first.pos); 
      } 
    } else {
      // We will be extending the current set by our value. We just need to check whether it will be the
      // new first value (in which case we need to mangle it's val) or not (in which case we mangle the first's val).
      movement newmovement;
      newmovement.pos = i;
      if (current.empty()) {
        // OK, this is the easy case, we select our new thing :)
        newmovement.val = Add(optimalplay, newB) - Add(optimalplay, newA);
        assert(newmovement.val > 0LL);
        current.insert(newmovement); 
      } else {
        if (Add(optimalplay, newB) > Add(optimalplay + current.begin()->val, newA)) {
          // In this case, the vertical movement is worse than the horizontal.
          newmovement.val = newB - newA;
          assert(newmovement.val >= current.begin()->val);
          current.insert(newmovement);
          movement updatedfirst = *(current.begin());
          assert(updatedfirst.pos < i);
          current.erase(current.begin());
          ll newval = Add(optimalplay + updatedfirst.val, newA) - Add(optimalplay, newA);
          assert(newval > 0LL);
          assert(newval <= updatedfirst.val);
          assert(newval <= newmovement.val);
          updatedfirst.val = newval;
          current.insert(updatedfirst); 
        } else {
          // In this case, we choose the vertical movement - so no change to the current first, just need to mangle
          // the new first correctly.
          newmovement.val = Add(optimalplay, newB) - Add(optimalplay, newA);
          assert(newmovement.val > 0LL);
          assert(newmovement.val <= newB - newA);
          assert(newmovement.val <= current.begin()->val);
          current.insert(newmovement);
        }
      }
    }
    optimalplay = Add(optimalplay, newA); 
    lastinset = Add(lastinset, newB);
    while (lastinset > maxval) {
      if (current.empty()) {
        assert(optimalplay == lastinset);
        return false;
      }
      movement ditched = *(current.rbegin());
      current.erase(--current.end()); // You can't erase a reverse_iterator, which is annoying. 
      lastinset -= ditched.val;
      output[ditched.pos] = Achar;
      rejected += 1;
    } 
    if (rejected > K) return false; 
  } 
  return true;
}



int main() {
  scanf("%d %d", &N, &K);
  for (int i = 0; i < N; ++i) scanf("%" PRId64, &inputA[i]);
  for (int i = 0; i < N; ++i) scanf("%" PRId64, &inputB[i]);

  int inputAbetter = 0;
  for (int i = 0; i < N; ++i) if (inputA[i] < inputB[i]) inputAbetter++;
  // A are the ones I want to take, B are the ones I have to take (N-K) of (I can drop at most K).
  A = inputA;
  B = inputB;
  Achar = 'A';
  Bchar = 'B'; 
  if (inputAbetter < K) {
    B = inputA;
    A = inputB;
    K = N - K;
    Achar = 'B';
    Bchar = 'A';
  } 
 
  // We binary search to find the lowest value we can limit the thieves to.
  // We know we can limit them to N * 10**9 (trivially, by doing whatever).
  // We also know we can't limit them to -1.
  // At every point, lo can't be achieved, while hi can. 
  ll lo = -1;
  ll hi = N * 1000000000LL;
  while (hi - lo > 1) {
    ll med = (hi + lo) / 2;
    if (CanAchieve(med)) {
      hi = med;
    } else {
      lo = med;
    }
  }  
  int Bcount = 0;
  for (int i = 0; i < N; ++i) {
    output[i] = 'C';
    if (B[i] < A[i]) {
      output[i] = Bchar; 
      Bcount += 1;
    }
  }
  assert(CanAchieve(hi));  // Just to fill in the Achars - the ones we dropped.
  for (int i = 0; i < N; ++i) {
    if (output[i] == 'C' && Bcount < (N-K)) {
      output[i] = Bchar;
      Bcount += 1;
    } else { 
      if (output[i] == 'C') output[i] = Achar;
    }
  }  
//  printf("%d %d %d\n", N, K, Bcount);
  assert(Bcount == N - K);
  output[N] = 0;
  printf("%" PRId64 "\n%s\n", hi, output);
  return 0;
}