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
#include <bits/stdc++.h>
#define ll long long
#define mp make_pair
#define fi first
#define se second
#define pb push_back
#define pi pair<int, int>
#define mod 1000000007
template<typename T> bool chkmin(T &a, T b){return (b < a) ? a = b, 1 : 0;}
template<typename T> bool chkmax(T &a, T b){return (b > a) ? a = b, 1 : 0;}
ll ksm(ll a, ll b) {if (b == 0) return 1; ll ns = ksm(a, b >> 1); ns = ns * ns % mod; if (b & 1) ns = ns * a % mod; return ns;}
using namespace std;
const int maxn = 100005;
int n;
int k; // can decr at most k times
int a[maxn], decr[maxn]; //
int md[maxn];

struct ST {
    int l, r;
    ST *ch[2];
    ll mx, lz; // lz : plus mx : range max
    ll mxr; // max after the mxdecr
    pi mxdecr; // first: value second: place
    pi rec; // right most
}p[maxn << 1], *root;
int stcnt = 0;
void bdtree(ST *a, int l, int r) {
    a->l = l, a->r = r;
    a->mx = a->lz = 0; a->mxdecr = mp(0, r);
    a->mxr = 0, a->rec = mp(0, r);
    if (l == r) return ;
    int mid = (l + r) >> 1;
    a->ch[0] = &p[stcnt++], a->ch[1] = &p[stcnt++];
    bdtree(a->ch[0], l, mid), bdtree(a->ch[1], mid + 1, r);
}
void upd(ST *a) {
    a->mx = max(a->ch[0]->mx, a->ch[1]->mx);
    a->mxdecr = max(a->ch[0]->mxdecr, a->ch[1]->mxdecr);
    if (a->mxdecr == a->ch[1]->mxdecr)
        a->mxr = a->ch[1]->mxr;
    else 
        a->mxr = max(a->ch[1]->mx, a->ch[0]->mxr);
    a->rec = a->ch[1]->rec;
}
void actlz(ST *a, ll x) {
    a->lz += x;
    a->mx += x;
    a->mxr += x;
}
void pushdown(ST *a) {
    if (a->lz) {
        for (int i = 0; i < 2; i++)
            actlz(a->ch[i], a->lz);
        a->lz = 0;
    }
}
void push(ST *a, int l, int r, ll x) {
    if (a->l == l && a->r == r) {
        actlz(a, x);
        return ;
    }
    pushdown(a);
    int mid = (a->l + a->r) >> 1;
    if (r <= mid) push(a->ch[0], l, r, x);
    else if (l > mid) push(a->ch[1], l, r, x);
    else push(a->ch[0], l, mid, x), push(a->ch[1], mid + 1, r, x);
    upd(a);
}
void chd(ST *a, int pl, pi dc) {
    if (a->l == a->r) {
        a->mxdecr = dc;
        a->rec = dc;
        return ;
    }
    pushdown(a);
    int mid = (a->l + a->r) >> 1;
    if (pl <= mid) chd(a->ch[0], pl, dc);
    else chd(a->ch[1], pl, dc);
    upd(a);
}
ll qmx(ST *a, int l, int r) {
    if (a->l == l && a->r == r) return a->mx;
    pushdown(a);
    int mid = (a->l + a->r) >> 1;
    if (r <= mid) return qmx(a->ch[0], l, r);
    else if (l > mid) return qmx(a->ch[1], l, r);
    else return max(qmx(a->ch[0], l, mid), qmx(a->ch[1], mid + 1, r));
}
pi qdecr(ST *a, int l, int r) {
    if (a->l == l && a->r == r) return a->mxdecr;
    pushdown(a);
    int mid = (a->l + a->r) >> 1;
    if (r <= mid) return qdecr(a->ch[0], l, r);
    else if (l > mid) return qdecr(a->ch[1], l, r);
    else return max(qdecr(a->ch[0], l, mid), qdecr(a->ch[1], mid + 1, r));
}

ll nmx;
ll rch; pi rs;
int findlst(ST *a) {
    if (a->l == a->r) return a->l;
    pushdown(a);
    // check if in left
    pi cc = max(a->ch[0]->rec, a->ch[1]->mxdecr);
    int flag = 0; ll cur = rch;
    if (chkmax(cc, rs)) 
        flag = 1;
    else if (cc == a->ch[1]->mxdecr) 
        cur = max(cur, a->ch[1]->mxr);
    else 
        cur = max(cur, a->ch[1]->mx);
    if (cur <= max(0ll, nmx - cc.fi)) {
        chkmax(rch, a->ch[1]->mx);
        chkmax(rs, a->ch[1]->mxdecr);
        return findlst(a->ch[0]);
    }
    else return findlst(a->ch[1]);
}
int tp[maxn];
bool chk(ll x, int k) {
    stcnt = 0;
    root = &p[stcnt++];
    bdtree(root, 1, n + 1);
    memset(tp, 0, sizeof(tp));
    for (int i = 1; i <= n; i++) {
        push(root, 1, i, a[i]);
        chd(root, i, mp(decr[i], i));
        while (root->mx > x && k) {
            //cout << root->mx << ' ' << k << endl;
            nmx = root->mx;
            rch = 0, rs = mp(0, 0);
            int l = findlst(root);
            k--;
            if (l > 1 && qmx(root, l, i) <= root->mx - qdecr(root, l, i).fi)
                l -= 1;
            pi qd = qdecr(root, l, i);
            //cout << "UPD " << l << ' ' <<qd.se << ' ' << qd.fi << endl;
            tp[qd.se] = 1;
            push(root, 1, qd.se, -qd.fi);
            chd(root, qd.se, mp(0, qd.se));
        }
        if (root->mx > x) return 0;
    }
    return 1;
}
ll ans;
ll s[maxn];
ll cals() {
    ll sum = 0;
    ll pmn = 0;
    ll nans = 0;
    for (int i = 1; i <= n; i++) {
        sum += s[i];
        chkmin(pmn, sum);
        chkmax(nans, sum - pmn);
    }
    return nans;
}
void solve(int k) {
    // cout << k << endl;
    // for (int i = 1; i <= n; i++)
    //     cout << a[i] << ' ' << decr[i] << endl;
    // cout << chk(3, k) << endl;
    // while (1);
    ll l = 0, r = 1e15;
    while (l < r) {
        ll mid = (l + r) >> 1;
        if (chk(mid, k)) {
            for (int i = 1; i <= n; i++)
                s[i] = a[i] - tp[i] * decr[i];
            r = cals();
        }
        else l = mid + 1;
    }
    chk(l, k);
    ans = l;
    cout << l << endl;
    int cnt = 0;
    for (int i = 1; i <= n; i++) 
        cnt += tp[i];
    for (int i = 1; i <= n; i++)
        if (tp[i] == 0 && cnt < k && decr[i] >= 0)
            tp[i] = 1, cnt += 1;
}
int f[2][maxn];
int ntp[maxn];

int main() {  
    // freopen("3a(1).in", "r", stdin);
    // freopen("new.out", "w", stdout);
    cin >> n;
    int m;
    cin >> m;
    int base = 0;
    int cnt[2] = {0, 0};
    for (int i = 0; i < 2; i++)
        for (int j = 1; j <= n; j++) 
            scanf("%d", &f[i][j]);
    for (int i = 1; i <= n; i++) {
        if (f[0][i] <= f[1][i]) ntp[i] = 0;
        else ntp[i] = 1;
        cnt[ntp[i]] += 1;
    }
    if (cnt[0] > m) {
        base = 1;
        for (int i = 1; i <= n; i++)
            decr[i] = f[1][i] - f[0][i], a[i] = f[1][i];
        solve(m);
    }
    else {
        base = 0;
        for (int i = 1; i <= n; i++)
            decr[i] = f[0][i] - f[1][i], a[i] = f[0][i];
        solve(n - m);
    }
    for (int i = 1; i <= n; i++) {
        tp[i] ^= base;
        printf("%c", 'A' + tp[i]);
    }
    int tot = 0;
    for (int i = 1; i <= n; i++)
        tot += (tp[i] == 0);
    assert(tot == m);
    for (int i = 1; i <= n; i++)
        s[i] = f[tp[i]][i];
    assert(ans == cals());
    //cerr << 1.0 * clock() / CLOCKS_PER_SEC << endl;
    return (0-0); // <3 cxr
}