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

#define all(X) (X).begin(), (X).end()
#define st first
#define nd second
typedef long long ll;
typedef pair<int,int> pii;

ll get(vector<int> tab) {
    ll mini = 0, res = 0,v=0;
    for(auto a:tab) {
        v += a;
        mini = min(mini, v);
        res = max(res, v - mini);
    }
    return res;
}

void wzo(vector<int> a, vector<int> b, int k) {
    int n = a.size();
    vector<int> mode(n), tab(n);

    for(int i=0;i<n;i++) {
        if(a[i] < b[i])
            mode[i] = 0;
        else
            mode[i] = 1;
        tab[i] = min(a[i],b[i]);
    }

    vector<int> canChange = mode;
    if(count(all(mode),1) < k)
        for(auto &v : canChange) v ^= 1; 
    int s = abs(count(all(mode),1) - k);

    vector<ll> ps(all(tab));
    for(int i=1;i<n;i++)
        ps[i] += ps[i-1];

    auto changes = [&](ll x)->multiset<pair<ll, int> > {
        ll base = x;
        multiset<pair<ll, int> > diffs;
        ll sum = 0;
        ll curMin = 0;
        for(int i=0;i<n; i++) {
            if(canChange[i]) {
                ll c = abs(a[i] - b[i]);
                diffs.insert({c,i});
                sum += c;
            }
            curMin = min(curMin, ps[i]);
            while(diffs.size() > 0 && base-sum < ps[i]) {
                auto it = diffs.end();
                it--;
                sum -= it->st;
                diffs.erase(it);
            }
            if(diffs.size() == 0 && base < ps[i])
                return {};

            auto it = diffs.lower_bound({1,-1e18});
            if(base > x+curMin) {
                ll v = base - x-curMin;
                while(v > 0 && it != diffs.end()) {
                    ll xd = min(v, it->st);
                    v -= xd;
                    sum -= xd;
                    ll d = it->st - xd;
                    int idx = it->nd;

                    auto it2 = it++;
                    diffs.erase(it2);
                    diffs.insert({d, idx});
                }
                base = x+curMin;
            }  
        }
        return diffs;
    };
    ll low = get(tab), high = 1e14+6, mid,lg = -1;
    multiset<pair<ll, int> > lgRes;
    while(low <= high) {
        mid = (low+high)/2;
        auto res = changes(mid);
        if(res.size() >= s) {
            high = mid-1;
            lg = mid;
            lgRes = res;
        }
        else {
            low = mid+1;
        }
    }

    cout<<lg<<"\n";
    vector<int> used;
    auto it = lgRes.begin();

    for(int i=0;i<s;i++) {
        mode[it->nd] ^= 1;
        tab[it->nd] = mode[it->nd] == 1 ? b[it->nd] : a[it->nd];
        it++;
    }
    // assert(count(all(mode),1) == k);
    // assert(get(tab) == lg);
    for(auto a:mode)
        cout<<(a ? 'B' : 'A');
}

int32_t main(){
    ios::sync_with_stdio(false); cin.tie(0);
    int n,k;
    cin >> n >> k;
    vector<int> a(n), b(n);
    for(int i=0;i<n;i++)
        cin >> a[i];
    for(int i=0;i<n;i++)
        cin >> b[i];

    wzo(a,b,n-k);
}