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

using namespace std;
using LL = long long;

#define st first
#define nd second
#define PII pair <int, int>

const int N = 100'007;
const LL MAX_RESULT = 1'001'002'003'004'005;

int n, k;
int a[N], b[N];

int start_k;
LL start[N];
int canChange[N];

char ans[N];
vector <int> clicked;

bool check(LL v)
{
    clicked.clear();

    int need = abs(k - start_k);
    LL base = 0, all_sum = 0, pref = 0;
    set <PII> diffs;

    for (int i = 1; i <= n; ++i) {
        pref += start[i];
        if (canChange[i] != -1) {
            all_sum += canChange[i];
            diffs.insert({canChange[i], i});
        }

        while (diffs.size() > 0 && base - (*diffs.begin()).st >= pref) {
            need--;
            base -= (*diffs.begin()).st;
            all_sum -= (*diffs.begin()).st;
            clicked.push_back((*diffs.begin()).nd);
            diffs.erase(diffs.begin());
        }

        if (base > pref) {
            if (diffs.size() != 0) {
                auto [val, id] = *diffs.begin();
                diffs.erase(diffs.begin());
                diffs.insert({val - (base - pref), id});
                all_sum -= base - pref;
            }

            base = pref;
        }

        if (base + v < pref) {
            return false;
        }

        while (base - all_sum + v < pref) {
            all_sum -= (*diffs.rbegin()).st;
            diffs.erase(--diffs.end());
        }
    }

    for (auto [_, id]: diffs) {
        clicked.push_back(id);
    }

    need -= diffs.size();
    return need <= 0;
}

void check_ans(LL v)
{
    int cnt = 0;
    LL best = 0, cur = 0;
    for (int i = 1; i <= n; ++i) {
        if (ans[i] == 'A') {
            ++cnt;
            cur += a[i];
        } else {
            assert(ans[i] == 'B');
            cur += b[i];
        }

        cur = max(cur, 0LL);
        best = max(best, cur);
    }

    assert(cnt == k && best == v);
}

int main()
{
    scanf("%d %d", &n, &k);
    for (int i = 1; i <= n; ++i) {
        scanf("%d", &a[i]);
    }

    for (int i = 1; i <= n; ++i) {
        scanf("%d", &b[i]);
    }

    for (int i = 1; i <= n; ++i) {
        if (a[i] < b[i]) {
            start[i] = a[i];
            start_k++;
        } else {
            start[i] = b[i];
        }
    }

    for (int i = 1; i <= n; ++i) {
        canChange[i] = -1;
        if (b[i] <= a[i] && start_k < k) {
            canChange[i] = a[i] - b[i];
        }

        if (a[i] < b[i] && k < start_k) {
            canChange[i] = b[i] - a[i];
        }
    }

    LL s = 0, e = MAX_RESULT;
    while (s < e) {
        LL mid = (s + e) / 2;
        if (check(mid)) {
            e = mid;
        } else {
            s = mid + 1;
        }
    }

    assert(check(s));
    printf("%lld\n", s);

    for (int i = 1; i <= n; ++i) {
        ans[i] = a[i] < b[i] ? 'A' : 'B';
    }

    int need = abs(start_k - k);
    for (int i = 0; i < need; ++i) {
        int p = clicked[i];
        ans[p] = ans[p] == 'A' ? 'B' : 'A';
    }

    for (int i = 1; i <= n; ++i) {
        printf("%c", ans[i]);
    }

    puts("");
    check_ans(s);
    return 0;
}