#include<bits/stdc++.h> using namespace std; typedef long long lld; constexpr int MAX_N = 100000; constexpr lld LINF = 1ll << 60; lld sm[MAX_N+9], dp[MAX_N+9]; int A[MAX_N+9], B[MAX_N+9]; int n,k; lld mn = LINF; char s[MAX_N+9]; int f(int i, int a) { if(i == n) { if(dp[n] < mn) { mn = dp[n]; return 1; } else { return 0; } } int bol = 0; if(a < k) { sm[i+1] = max(0ll, sm[i] + A[i]); dp[i+1] = max(dp[i], sm[i+1]); if(f(i+1, a+1)) { s[i] = 'A'; bol = 1; } } if(i-a < n-k) { sm[i+1] = max(0ll, sm[i] + B[i]); dp[i+1] = max(dp[i], sm[i+1]); if(f(i+1, a)) { s[i] = 'B'; bol = 1; } } return bol; } int main() { scanf("%d%d", &n, &k); for(int i = 0; i < n; ++i) { scanf("%d", &A[i]); } for(int i = 0; i < n; ++i) { scanf("%d", &B[i]); } f(0, 0); printf("%lld\n", mn); printf("%s\n", s); }
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 | #include<bits/stdc++.h> using namespace std; typedef long long lld; constexpr int MAX_N = 100000; constexpr lld LINF = 1ll << 60; lld sm[MAX_N+9], dp[MAX_N+9]; int A[MAX_N+9], B[MAX_N+9]; int n,k; lld mn = LINF; char s[MAX_N+9]; int f(int i, int a) { if(i == n) { if(dp[n] < mn) { mn = dp[n]; return 1; } else { return 0; } } int bol = 0; if(a < k) { sm[i+1] = max(0ll, sm[i] + A[i]); dp[i+1] = max(dp[i], sm[i+1]); if(f(i+1, a+1)) { s[i] = 'A'; bol = 1; } } if(i-a < n-k) { sm[i+1] = max(0ll, sm[i] + B[i]); dp[i+1] = max(dp[i], sm[i+1]); if(f(i+1, a)) { s[i] = 'B'; bol = 1; } } return bol; } int main() { scanf("%d%d", &n, &k); for(int i = 0; i < n; ++i) { scanf("%d", &A[i]); } for(int i = 0; i < n; ++i) { scanf("%d", &B[i]); } f(0, 0); printf("%lld\n", mn); printf("%s\n", s); } |