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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#pragma GCC optimize("O3,unroll-loops")
/* #pragma GCC optimize("Ofast,unroll-loops") */
// wtf, tera jest 1.37s. czemu
/* #pragma GCC optimize("O3,unroll-loops,prefetch-loop-arrays") */
// slower z tą pragmą bez własnych optow interestingly.
// -mprefetchwt1 todo -> to czesc avx512 though?
/* #pragma GCC target("prefetch-loop-arrays") */
/* #pragma GCC target("avx2") */
/* #pragma GCC optimize("Ofast,unroll-loops") */
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pb push_back
#define mp make_pair
#define all(a) begin(a),end(a)
#define FOR(x,val,to) for(int x=(val);x<int((to));++x)
#define FORE(x,val,to) for(auto x=(val);x<=(to);++x)
#define FORR(x,arr) for(auto &x: arr)
#define FORS(x,plus,arr) for(auto x = begin(arr)+(plus); x != end(arr); ++x)
#define FORREV(x,plus,arr) for(auto x = (arr).rbegin()+(plus); x !=(arr).rend(); ++x)
#define REE(s_) {cout<<s_<<'\n';exit(0);}
#define GET(arr) for(auto &i: (arr)) sc(i)
#define whatis(x) cerr << #x << " is " << (x) << endl;
#define e1 first
#define e2 second
#define INF 0x7f7f7f7f
typedef std::pair<int,int> pi;
typedef std::vector<int> vi;
typedef std::vector<std::string> vs;
typedef int64_t ll;
typedef uint64_t ull;
#define umap unordered_map
#define uset unordered_set
using namespace std;
using namespace __gnu_pbds;

#ifdef ONLINE_JUDGE
#define whatis(x) ;
#endif
#ifdef _WIN32
#define getchar_unlocked() _getchar_nolock()
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
template<class L, class R> ostream& operator<<(ostream &os, map<L, R> P) { for(auto const &vv: P)os<<"("<<vv.first<<","<<vv.second<<")"; return os; }
template<class T> ostream& operator<<(ostream &os, set<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class T> ostream& operator<<(ostream &os, vector<T> V) { os<<"[";for(auto const &vv:V)os<<vv<<","; os<<"]"; return os; }
template<class L, class R> ostream& operator<<(ostream &os, pair<L, R> P) { os<<"("<<P.first<<","<<P.second<<")"; return os; }
inline int fstoi(const string &str){auto it=str.begin();bool neg=0;int num=0;if(*it=='-')neg=1;else num=*it-'0';++it;while(it<str.end()) num=num*10+(*it++-'0');if(neg)num*=-1;return num;}
inline void getch(char &x){while(x = getchar_unlocked(), x < 33){;}}
inline void getstr(string &str){str.clear(); char cur;while(cur=getchar_unlocked(),cur<33){;}while(cur>32){str+=cur;cur=getchar_unlocked();}}
template<typename T> inline bool sc(T &num){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){if(c == EOF) return false;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; return true;}template<typename T, typename ...Args> inline void sc(T &num, Args &...args){ bool neg=0; int c; num=0; while(c=getchar_unlocked(),c<33){;} if(c=='-'){ neg=1; c=getchar_unlocked(); } for(;c>47;c=getchar_unlocked()) num=num*10+c-48; if(neg) num*=-1; sc(args...); }
template<typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>; //s.find_by_order(), s.order_of_key() <- works like lower_bound
template<typename T> using ordered_map = tree<T, int, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 1000001

/* #define MXN */
// wa z tym^^ for some reason..
// -> przez to że dodawałem -1 zamiast jakiegoś -LL_MAX / 2.
// guess best just wywalić MXN.
#define PRINTS
#define assert(x) ;
/* #define VERIFY */
#define whatis(x) ;

int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);
    /* assert(0); */
    /* constexpr int mxn = 10000; */
    /* constexpr int mxn = 10000, mxk = 5000; */
    /* constexpr int mxn = 10000; */
    /* constexpr int mxn = 20000; */
    /* constexpr int mxk = 32 * 313 + 3; */
    /* constexpr int mxk = 5000; */
    /* constexpr int mxk = 16 * 312 + 3; */ // wtf, czemu przez wiekszy mxk nie działa ? xd
    // -> to było < 5000 xdd.
    /* constexpr int mxn = 10000; */
    /* constexpr int mxk = 16 * 313 + 3; */
    /* constexpr int mxk = 16 * 313 + 3; */
    /* constexpr int mxk = 16 * 313 + 1; */
    // +1, +3 both dobrze.
    // +2, +4 bad.
    // todo podzielne przez 64?
    constexpr int mxn = 100000;
    constexpr int mxk = 50000;
    int n,k;
    sc(n,k);
#ifdef MXN
    /* int _a[mxn], _b[mxn]; */
    int _a[mxn], _b[mxn];
    // XDDD, kox bug też.
    memset(_a,-1,sizeof _a);
    memset(_b,-1,sizeof _b);
    FOR(i,0,n){
        sc(_a[i]);
    }
    FOR(i,0,n){
        sc(_b[i]);
    }
#else
    int _a[n], _b[n];
    GET(_a);
    GET(_b);
#endif
    // xd.
    /* const int *a = a; */
    /* const int *b = b; */
    const int *a = _a;
    const int *b = _b;
    bool swpd = 0;
    if(k > n - k){
        k = n - k;
        swap(a, b);
        swpd = 1;
    }
    // todo opt z high = sumą maxvali.
    // -> kox opt.
    /* ll low = 0, high = n * (ll)1000000000, mid, ans; */
    // ??? high = 0
    ll low = 0, high = 0, mid, ans;
    // XDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD
    /* FOR(i,0,n) */
    //* FOR(i,0,n) */
    /*     high += max(a[i], b[i]); // down to 1.5s from 1.7s with this opt. */
    FOR(i,0,n)
        high += max({0, a[i], b[i]}); // not overkill btw.
    /* whatis(high) */
    /* ll low = 4, high = 4, mid, ans = -1; */
    constexpr ll bad = 0x7f7f7f7f7f7f7f7f;
    while(low <= high){
        mid = (low + high) >> 1;
        const auto norm = [&](ll f){
            return f < 0 ? 0 : f > mid ? bad : f;
        };
        /* whatis(mid) */
        // todo n, ?k? constexpr; podzielne przez jakieś 8 najlepiej
        /* ll dp[mxk + 1]; */
        /* alignas(64) ll dp[mxk + 1]; // o eps slower btw z alignas(64). */
        /* alignas(16) ll dporig[mxk + 17]; // o eps slower btw z alignas(64). */
        vector<pair<ll,int>> vec;
        vec.emplace_back(0, 0);
        /* alignas(64) ll dporig[mxk + 17]; // o eps slower btw z alignas(64). */
        /* memset(dporig, 127, sizeof dporig); */
        /* ll *dp = dporig + 16; */
        /* alignas(16) ll dp[mxk + 1]; // o eps slower btw z alignas(64). */
        // z 16 eq speed.
        /* ll dp[mxk + 1]; */
        /* ll nxdp[mxk + 1]; */
        /* memset(dp, -1, sizeof dp); */
        // XXX don't forget this memset xddddd
        /* alignas(16) ll dp[mxk + 1]; // o eps slower btw z alignas(64). */
        /* memset(dp, 127, sizeof dp); */
        /* // and don't forget to set 0 as well xd. */
        /* dp[0] = 0; */
        /* int cl = -1, cr = -1; */
        /* int cl = 0, cr = mxk; */
        /* ll d1sum = 0, d2sum = 0; */
        /* ll difsum = 0; */
        /* vector<pair<ll,int>> nwvec; */
        /* nwvec.reserve(100); */
#ifdef MXN
        for(int i = 0; i < mxn; ++i){
#else
        for(int i = 0; i < n; ++i){
#endif
            /* set<int> iledif; */
            const int cra = a[i]; // k + 1
            const int crb = b[i]; // k
            int nxk = mxk;
            /* for(; nxk > 3;){ */
            vector<pair<ll,int>> nwvec;
            nwvec.emplace_back(norm(vec[0].first + crb), 0);
            /* nwvec = {{norm(vec[0].first + crb), 0}}; */
            /* int cl = 0; */
            auto nwvecemplace = [&](ll val, int tillr){
                if(nwvec.back().first == val)
                    nwvec.back().second = tillr;
                else
                    nwvec.emplace_back(val, tillr);
            };
            for(int i = 0; i < vec.size(); ++i){
                /* if( */
                /* if(cl != vec[i].second){ */
                if(nwvec.back().second != vec[i].second){
                    /* nwvec.emplace_back(norm(vec[i].first + min(cra, crb)), vec[i].second); */
                    nwvecemplace(norm(vec[i].first + min(cra, crb)), vec[i].second);
                }
                if(vec[i].second != mxk){
                    if(i + 1 == vec.size())
                        /* nwvec.emplace_back(norm(vec[i].first + cra), vec[i].second + 1); */
                        nwvecemplace(norm(vec[i].first + cra), vec[i].second + 1);
                    else
                        /* nwvec.emplace_back(norm(min(vec[i].first + cra, vec[i + 1].first + crb)), vec[i].second + 1); */
                        nwvecemplace(norm(min(vec[i].first + cra, vec[i + 1].first + crb)), vec[i].second + 1);
                }
            }
            vec = std::move(nwvec);
            whatis(vec)
            /* for(; nxk > 3;){ */
            /* /1* for(; nxk >= 3;){ *1/ */
            /*     // wtf, tera jest 1.37s. czemu */
            /*     // ale zajebioza. */
            /*     // ale czemu, wczesniej tak dobrze nie było, miałem jakies 1.6s xd. */
            /*     // jest bez alignas btw. */
            /*     /1* __builtin_prefetch(&dp[nxk - 3], 1); *1/ */
            /*     // to samo, ale bez prefetchu 1.7s xd (vs 1.5s sam loop poniżej) */
            /*     __builtin_prefetch(&dp[nxk - 3], 1); */
            /*     /1* __builtin_prefetch(&dp[nxk - 3], 0); *1/ */
            /*     // ten sam czas gdy either 0 lub 1, ale 1.5s gdy oba. */
            /*     // n20k -> 6.6s no prefetch (and no this loop) vs 5.4s prefetch */
            /*     dp[nxk] = min(dp[nxk] + crb, dp[nxk - 1] + cra); */
            /*     if(dp[nxk] < 0) */
            /*         dp[nxk] = 0; */
            /*     else if(dp[nxk] > mid) */
            /*         dp[nxk] = bad; */
            /*     --nxk; */
            /*     dp[nxk] = min(dp[nxk] + crb, dp[nxk - 1] + cra); */
            /*     if(dp[nxk] < 0) */
            /*         dp[nxk] = 0; */
            /*     else if(dp[nxk] > mid) */
            /*         dp[nxk] = bad; */
            /*     --nxk; */
            /*     dp[nxk] = min(dp[nxk] + crb, dp[nxk - 1] + cra); */
            /*     if(dp[nxk] < 0) */
            /*         dp[nxk] = 0; */
            /*     else if(dp[nxk] > mid) */
            /*         dp[nxk] = bad; */
            /*     --nxk; */
            /*     dp[nxk] = min(dp[nxk] + crb, dp[nxk - 1] + cra); */
            /*     if(dp[nxk] < 0) */
            /*         dp[nxk] = 0; */
            /*     else if(dp[nxk] > mid) */
            /*         dp[nxk] = bad; */
            /*     --nxk; */
            /* } */
            /* for(; nxk > 0; --nxk){ */
            /*     dp[nxk] = min(dp[nxk] + crb, dp[nxk - 1] + cra); */
            /*     if(dp[nxk] < 0) */
            /*         dp[nxk] = 0; */
            /*     else if(dp[nxk] > mid) */
            /*         dp[nxk] = bad; */
            /* } */
            /* dp[0] = dp[0] + crb; */
            /* if(dp[0] < 0) */
            /*     dp[0] = 0; */
            /* else if(dp[0] > mid) */
            /*     dp[0] = bad; */
            /* for(int c = 0; c <= mxk; ++c) */
            /* /1* for(int c = 0; c <= k; ++c) *1/ */
            /*     iledif.insert(dp[c]); */
            /* /1* difsum += iledif.size(); *1/ */
            /* whatis(iledif.size()) */
            /* /1* /2* whatis(nwvec.size()) *2/ *1/ */
            /* whatis(vec.size()) */
            /* /1* whatis(vec) *1/ */
            /* /1* whatis(iledif) *1/ */
            /* /1* for(int c = 0; c <= k; ++c) *1/ */
            /* /1*     cout << dp[c] << ' '; *1/ */
            /* /1* cout << endl; *1/ */
            /* /1* assert(vec.size() == iledif.size()); *1/ */
            /* if(vec.size() > iledif.size() + 4){ */
            /* /1* if(vec.size() != iledif.size()){ *1/ */
            /*     whatis(vec) */
            /*     whatis(iledif) */
            /* } */
            // kozak, doesn't trigger now.
        }
        // not overkill at all.
        /* ll vecres = lower_bound(all(vec), k, [](auto &f, auto &s){return f < s.second;})->second; */
        /* ll vecres = (upper_bound(all(vec), k, [](auto &f, auto &s){return s > f.second;}) - 1)->second; */
        /* ll vecres = (upper_bound(all(vec), k, [](auto &f, auto &s){return f < s.second;}) - 1)->second; */
        /* assert(vecres == dp[k]); */
        ll vecres = -1;
        for(auto &i: vec){
            if(i.second >= k){
                vecres = i.first;
                break;
            }
        }
        assert(vecres != -1);
        /* assert(vecres == dp[k]); */
        /* whatis(difsum) */
        /* if(dp[k] <= mid){ */
        whatis(vec)
        whatis(mid)
        whatis(vecres)
        whatis(vecres <= mid)
        if(vecres <= mid){
            ans = mid;
            high = mid - 1;
        }
        else{
            low = mid + 1;
        }
    }
#ifdef PRINTS
    static vector<pair<int, bool>> p[mxn];
    /* static vector<pair<ll, int>> pastvec[mxn]; */
    {
        /* vector<pair<int, bool>> p[maxn + 1]; */
        // omg, wait, czy bug przez to że tutaj porównywałem do mid vs ans? xdddd
        // (ale jak w legacy wersji było w takim razie dobrze, skoro tam tak samo..)
        // -> still źle, ale at least one future bug less.
        const auto norm = [&](ll f){
            /* return f < 0 ? 0 : f > mid ? bad : f; */
            return f < 0 ? 0 : f > ans ? bad : f;
        };
        vector<pair<ll,int>> vec;
        vec.emplace_back(0, 0);
#ifdef MXN
        for(int i = 0; i < mxn; ++i){
#else
        for(int i = 0; i < n; ++i){
#endif
            /* pastvec[i] = vec; */
            /* set<int> iledif; */
            const int cra = a[i]; // k + 1
            const int crb = b[i]; // k
            int nxk = mxk;
            /* for(; nxk > 3;){ */
            vector<pair<ll,int>> nwvec;
            nwvec.emplace_back(norm(vec[0].first + crb), 0);
            p[i].emplace_back(0, 0);
            /* int cl = 0; */
            auto nwvecemplace = [&](ll val, int tillr, bool nwp){
                /* whatis("--nwvecemplace") */
                /* whatis(val) */
                /* whatis(tillr) */
                /* whatis(nwp) */
                /* whatis("--nwvecemplace") */
                // prevy chyba wciąż mogą być jednak inne, więc tak czy inaczej
                // muszę zrobić push_back.
                // ewentualnie opt że jak nwp równe p[i].back().second to nie muszę.
                // guess nawet oddzielnie to mogę btw.
                if(nwvec.back().first == val){
                    nwvec.back().second = tillr;
                    /* p[i].emplace_back(tillr, nwp); */
                }
                else{
                    nwvec.emplace_back(val, tillr);
                    /* p[i].emplace_back(tillr, nwp); */
                }
                if(p[i].back().second == nwp){
                    p[i].back().first = tillr;
                }
                else{
                    p[i].emplace_back(tillr, nwp);
                }
            };
            /* whatis(cra) */
            /* whatis(crb) */
            for(int i = 0; i < vec.size(); ++i){
                /* whatis(i) */
                /* if( */
                /* if(cl != vec[i].second){ */
                if(nwvec.back().second != vec[i].second){
                    /* nwvec.emplace_back(norm(vec[i].first + min(cra, crb)), vec[i].second); */
                    // ohhh my goddd.
                    // jakim cudem tak prosty bug zajął mi O(1h) na
                    // znalezenie....
                    // patrzałem na wszystkie branche poza tym jednym ofc.
                    /* nwvecemplace(norm(vec[i].first + min(cra, crb)), vec[i].second, 0); */
                    if(cra < crb)
                        nwvecemplace(norm(vec[i].first + cra), vec[i].second, 1);
                    else
                        nwvecemplace(norm(vec[i].first + crb), vec[i].second, 0);
                    /* p[i].emplace_back(vec[i].second, 0); */
                }
                if(vec[i].second != mxk){
                    if(i + 1 == vec.size()){
                        /* nwvec.emplace_back(norm(vec[i].first + cra), vec[i].second + 1); */
                        /* nwvecemplace(norm(vec[i].first + cra), vec[i].second + 1); */
                        nwvecemplace(norm(vec[i].first + cra), vec[i].second + 1, 1);
                        /* p[i].emplace_back(vec[i].second + 1, 1); */
                    }
                    else{
                        /* nwvec.emplace_back(norm(min(vec[i].first + cra, vec[i + 1].first + crb)), vec[i].second + 1); */
                        if(vec[i].first + cra < vec[i + 1].first + crb){
                            /* nwvecemplace(norm(vec[i].first + cra), vec[i].second + 1); */
                            /* whatis("b1") */
                            nwvecemplace(norm(vec[i].first + cra), vec[i].second + 1, 1);
                            /* p[i].emplace_back(vec[i].second + 1, 1); */
                        }
                        else{
                            /* whatis("b2") */
                            nwvecemplace(norm(vec[i + 1].first + crb), vec[i].second + 1, 0);
                        }
                    }
                }
            }
            /* if(i <= n) */
            /* for(int x = 0; x < p[i].size(); ++x){ */
            /*     for(int j = x ? p[i][x - 1].first + 1 : 0; j <= p[i][x].first; ++j){ */
            /*         assert(j <= mxk); */
            /*         if(!j) */
            /*             continue; */
            /*         ll nwvecres = -1; */
            /*         for(auto &i: nwvec){ */
            /*             if(i.second >= j){ */
            /*                 nwvecres = i.first; */
            /*                 break; */
            /*             } */
            /*         } */
            /*         ll vecm1 = -1; */
            /*         for(auto &i: vec){ */
            /*             if(i.second >= j - 1){ */
            /*                 vecm1 = i.first; */
            /*                 break; */
            /*             } */
            /*         } */
            /*         ll vecm0 = -1; */
            /*         for(auto &i: vec){ */
            /*             if(i.second >= j){ */
            /*                 vecm0 = i.first; */
            /*                 break; */
            /*             } */
            /*         } */
            /*         /1* whatis(i) *1/ */
            /*         /1* whatis(p[i]) *1/ */
            /*         /1* whatis(vec[i]) *1/ */
            /*         /1* whatis(nwvec[i]) *1/ */
            /*         /1* whatis(vec) *1/ */
            /*         /1* whatis(nwvec) *1/ */
            /*         /1* whatis(x) *1/ */
            /*         /1* whatis(j) *1/ */
            /*         assert(nwvecres != -1); */
            /*         ll expres = norm(p[i][x].second ? vecm1 + cra : vecm0 + crb); */
            /*         /1* whatis(expres) *1/ */
            /*         /1* whatis(nwvecres) *1/ */
            /*         assert(nwvecres == expres); */
            /*     } */
            /* } */
            vec = std::move(nwvec);
        }
        ll vecres = -1;
        for(auto &i: vec){
            if(i.second >= k){
                vecres = i.first;
                break;
            }
        }
        assert(vecres != -1);
        // hmm.
        assert(vecres <= ans);
    }
    /* static bool p[mxn + 1][mxk + 1]; */
    /* { */
    /* /1* static unsigned short p[mxn][mxk]; *1/ */
    /* // todo bitset def. */
    /*     alignas(16) ll dp[mxk + 1]; // o eps slower btw z alignas(64). */
    /*     memset(dp, 127, sizeof dp); */
    /*     dp[0] = 0; */
/* #ifdef MXN */
    /*     for(int i = 0; i < mxn; ++i){ */
/* #else */
    /*     for(int i = 0; i < n; ++i){ */
/* #endif */
    /*         const int cra = a[i]; // k + 1 */
    /*         const int crb = b[i]; // k */
    /*         int nxk = mxk; */
    /*         for(; nxk > 0; --nxk){ */
    /*             /1* dp[nxk] = min(dp[nxk] + crb, dp[nxk - 1] + cra); *1/ */
    /*             if(dp[nxk - 1] + cra < dp[nxk] + crb){ */
    /*                 dp[nxk] = dp[nxk - 1] + cra; */
    /*                 p[i][nxk] = 1; */
    /*             } */
    /*             else{ */
    /*                 dp[nxk] = dp[nxk] + crb; */
    /*             } */
    /*             if(dp[nxk] < 0) */
    /*                 dp[nxk] = 0; */
    /*             else if(dp[nxk] > mid) */
    /*                 dp[nxk] = bad; */
    /*         } */
    /*         dp[0] = dp[0] + crb; */
    /*         if(dp[0] < 0) */
    /*             dp[0] = 0; */
    /*         else if(dp[0] > mid) */
    /*             dp[0] = bad; */
    /*     } */
    /* } */
#endif
    cout << ans << '\n';
#ifdef PRINTS
    string s;
    int crk = k;
    /* for(int crn = n; crn > 0; --crn){ */
    /* for(int crn = n - 1; crn >= 0; --crn){ */
    /*     s += "BA"[p[crn][crk]^swpd]; */
    /*     crk -= p[crn][crk]; */
    /* } */
    for(int crn = n - 1; crn >= 0; --crn){
        ll vecres = -1;
        /* whatis(crn) */
        /* whatis(crk) */
        /* whatis(pastvec[crn]) */
        /* whatis(p[crn]) */
        for(auto &i: p[crn]){
            if(i.first >= crk){
                vecres = i.second;
                break;
            }
        }
        assert(vecres != -1);
        s += "BA"[vecres^swpd];
        crk -= vecres;
    }
    reverse(all(s));
    whatis(s)
#ifdef VERIFY
    ll sum = 0;
    ll mx = 0;
    int ilek = 0;
    int it = 0;
    /* whatis(s) */
    /* whatis(ans) */
    FORR(i,s){
        /* if((i == 'A') ^ swpd){ */
        /* whatis(it) */
        /* whatis(sum) */
        if(i == 'A'){
            /* whatis(_a[it]) */
            /* if(!swpd) */
            ++ilek;
            sum += _a[it];
        }
        else{
            /* whatis(_b[it]) */
            /* if(swpd) */
            /*     ++ilek; */
            sum += _b[it];
        }
        /* whatis(it) */
        /* whatis(sum) */
        mx = max(mx, sum);
        sum = max(sum, 0l);
        ++it;
    }
    if(swpd)
        ilek = n - ilek;
    /* assert(sum == ans); */
    /* whatis(mx) */
    assert(mx == ans);
    assert(ilek == k);
#endif
    /* if(0) */
    cout << s << '\n';
#endif
    // 8m12s without opts and pragmas.
}