#include <bits/stdc++.h> using namespace std; const long long maxinf = 10000000000000000; long long wr[100005][2]; int n, k; long long dp[100005][2]; bool check(long long mxk) { dp[0][0] = 0; for (int i = 1; i <= k; i++) { dp[i][0] = maxinf; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { if (j == 0) dp[j][i & 1] = dp[j][(i ^ 1) & 1] + wr[i][1]; else dp[j][i & 1] = min(dp[j - 1][(i ^ 1) & 1] + wr[i][0], dp[j][(i ^ 1) & 1] + wr[i][1]); dp[j][i & 1] = max(dp[j][i & 1], (long long)0); if (dp[j][i & 1] > mxk) dp[j][i & 1] = maxinf; // cout << i << ' ' << j << ' ' << dp[j][i & 1] << '\n'; } } if (dp[k][n & 1] > mxk) return false; return true; } int main() { ios_base::sync_with_stdio(0); cin >> n >> k; // DPS dp[n + 2][k + 2]; long long s = 0; for (int i = 0; i < 2; i++) { for (int j = 1; j <= n; j++) { cin >> wr[j][i]; s += max((long long)0, wr[j][i]); } } long long dd = 0, gg = s, ss; // cout << check(4) << '\n'; // cout << "=====================\n"; // cout << check(3) << '\n'; // return 0; while (dd < gg) { ss = (dd + gg) / 2; // cout << ss << ' ' << check(ss) << '\n'; if (check(ss)) { gg = ss; } else dd = ss + 1; } cout << dd << '\n'; pair<long long, long long> dp2[n + 2][k + 2]{}; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { if (j == 0) dp2[i][j] = {dp2[i - 1][j].first + wr[i][1], 0}; else { if (dp2[i - 1][j - 1].first + wr[i][0] <= dp2[i - 1][j].first + wr[i][1]) { dp2[i][j] = {dp2[i - 1][j - 1].first + wr[i][0], i}; } else { dp2[i][j] = {dp2[i - 1][j].first + wr[i][1], dp2[i - 1][j].second}; } } // dp[j][i & 1] = min(dp[j - 1][(i ^ 1) & 1] + wr[i][0], dp[j][(i ^ 1) & 1] + wr[i][1]); dp2[i][j].first = max(dp2[i][j].first, (long long)0); if (dp2[i][j].first > ss) { dp2[i][j].first = maxinf; } // cout << i << ' ' << j << ' ' << dp[j][i & 1] << '\n'; } } int lop = n, lob2 = k; bool wh[n + 2]{}; while (lop>=0) { // cout<<dp2[lop][lob2].second<<'\n'<<flush; wh[dp2[lop][lob2].second] = 1; lop = dp2[lop][lob2].second - 1; lob2--; } for(int i=1;i<=n;i++){ cout<<(wh[i]?'A':'B'); } }
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 | #include <bits/stdc++.h> using namespace std; const long long maxinf = 10000000000000000; long long wr[100005][2]; int n, k; long long dp[100005][2]; bool check(long long mxk) { dp[0][0] = 0; for (int i = 1; i <= k; i++) { dp[i][0] = maxinf; } for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { if (j == 0) dp[j][i & 1] = dp[j][(i ^ 1) & 1] + wr[i][1]; else dp[j][i & 1] = min(dp[j - 1][(i ^ 1) & 1] + wr[i][0], dp[j][(i ^ 1) & 1] + wr[i][1]); dp[j][i & 1] = max(dp[j][i & 1], (long long)0); if (dp[j][i & 1] > mxk) dp[j][i & 1] = maxinf; // cout << i << ' ' << j << ' ' << dp[j][i & 1] << '\n'; } } if (dp[k][n & 1] > mxk) return false; return true; } int main() { ios_base::sync_with_stdio(0); cin >> n >> k; // DPS dp[n + 2][k + 2]; long long s = 0; for (int i = 0; i < 2; i++) { for (int j = 1; j <= n; j++) { cin >> wr[j][i]; s += max((long long)0, wr[j][i]); } } long long dd = 0, gg = s, ss; // cout << check(4) << '\n'; // cout << "=====================\n"; // cout << check(3) << '\n'; // return 0; while (dd < gg) { ss = (dd + gg) / 2; // cout << ss << ' ' << check(ss) << '\n'; if (check(ss)) { gg = ss; } else dd = ss + 1; } cout << dd << '\n'; pair<long long, long long> dp2[n + 2][k + 2]{}; for (int i = 1; i <= n; i++) { for (int j = 0; j <= k; j++) { if (j == 0) dp2[i][j] = {dp2[i - 1][j].first + wr[i][1], 0}; else { if (dp2[i - 1][j - 1].first + wr[i][0] <= dp2[i - 1][j].first + wr[i][1]) { dp2[i][j] = {dp2[i - 1][j - 1].first + wr[i][0], i}; } else { dp2[i][j] = {dp2[i - 1][j].first + wr[i][1], dp2[i - 1][j].second}; } } // dp[j][i & 1] = min(dp[j - 1][(i ^ 1) & 1] + wr[i][0], dp[j][(i ^ 1) & 1] + wr[i][1]); dp2[i][j].first = max(dp2[i][j].first, (long long)0); if (dp2[i][j].first > ss) { dp2[i][j].first = maxinf; } // cout << i << ' ' << j << ' ' << dp[j][i & 1] << '\n'; } } int lop = n, lob2 = k; bool wh[n + 2]{}; while (lop>=0) { // cout<<dp2[lop][lob2].second<<'\n'<<flush; wh[dp2[lop][lob2].second] = 1; lop = dp2[lop][lob2].second - 1; lob2--; } for(int i=1;i<=n;i++){ cout<<(wh[i]?'A':'B'); } } |