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
#include <cstdio>
#include <algorithm>
#include <unordered_map>
#include <vector>
#define MAX_N 100010
using namespace std;

typedef long long LL;

int n, k;
LL A[MAX_N], B[MAX_N];

class Solv {
public:
    char last;
    Solv* prev;
};

unordered_map<LL, vector<pair<pair<LL, LL>, Solv*>>*> wyns;
vector<pair<pair<LL, LL>, Solv*>> __empty;

void printSolv(Solv* solv, bool swapped) {
    if (solv->prev != NULL) {
        printSolv(solv->prev, swapped);
    }
    if (swapped) {
        printf("%c", solv->last == 'A' ? 'B' : 'A');
    } else {
        printf("%c", solv->last);
    }
}

LL serialize(int prefLen, int k) {
    return prefLen * 1000000000 + k;
}

// first: suffix, second: any
void genFight(int prefLen, int k) {
    LL rep = serialize(prefLen, k);
    LL a = A[prefLen - 1];
    LL b = B[prefLen - 1];

    if (prefLen == 1) {
        vector<pair<pair<LL, LL>,Solv*>>* v = new vector<pair < pair < LL, LL >, Solv * >>();
        Solv *solv = new Solv();
        solv->prev = NULL;
        if (k == 0) {
            solv->last = 'B';
            v->emplace_back(make_pair(max(b, 0ll), max(b, 0ll)), solv);
        } else {
            solv->last = 'A';
            v->emplace_back(make_pair(max(a, 0ll), max(a, 0ll)), solv);
        }
        wyns.insert(make_pair(rep, v));
        return;
    }

    vector < pair < pair < LL, LL >, Solv * >>* out = new vector<pair < pair < LL, LL >, Solv * >>();
    vector < pair < pair < LL, LL >, Solv * >>* realOut = new vector<pair < pair < LL, LL >, Solv * >>();

    auto ifA = wyns[serialize(prefLen - 1, k - 1)];
    if (ifA == NULL) {
        ifA = &__empty;
    }
    for (int i = 0; i < (int) ifA->size(); i++) {
        auto p = (*ifA)[i].first;
        Solv *solv = new Solv();
        solv->last = 'A';
        solv->prev = (*ifA)[i].second;
        auto p2 = make_pair(max(0ll, p.first + a), max(p.second, p.first + a));
        out->push_back(make_pair(p2, solv));
    }

    auto ifB = wyns[serialize(prefLen - 1, k)];
    if (ifB == NULL) {
        ifB = &__empty;
    }
    for (int i = 0; i < (int) ifB->size(); i++) {
        auto p = (*ifB)[i].first;
        Solv *solv = new Solv();
        solv->last = 'B';
        solv->prev = (*ifB)[i].second;
        auto p2 = make_pair(max(0ll, p.first + b), max(p.second, p.first + b));
        out->push_back(make_pair(p2, solv));
    }

    sort(out->begin(), out->end());
    realOut->push_back((*out)[0]);

    for (auto i : *out) {
        if (i.first.second < (*realOut)[realOut->size() - 1].first.second) {
            realOut->push_back(i);
        }
    }

    out->clear();

    wyns.insert(make_pair(rep, realOut));
}

int main() {
    scanf("%d%d", &n, &k);
    //n = 5000;
    //k = 2500;

    for (int i = 0; i < n; i++) {
        scanf("%lld", &A[i]);
        //A[i] = random() % 1000 - 500;
    }

    for (int i = 0; i < n; i++) {
        scanf("%lld", &B[i]);
        //B[i] = random() % 1000 - 500;
    }

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

    for (int i = 1; i <= n; i++) {
        int left = n - i;
        for (int j = max(0, k - left); j <= min(k, i); j++) {
            genFight(i, j);
        }
    }

    auto v = wyns[serialize(n, k)];
    printf("%lld\n", (*v)[v->size() - 1].first.second);
    printSolv((*v)[v->size() - 1].second, swapped);
}