#include <bits/stdc++.h> using namespace std; #define ll long long vector <int> anna; int n, k; ll res = LLONG_MAX; vector <vector <ll> > v; vector <bool> ress; void Check() { vector <bool> obrazy(n); for (int i = 0; i < k; i++) obrazy[anna[i]] = true; ll sum = 0, sumtemp = 0; for (int i = 0; i < n; i++) { if (sumtemp + v[!obrazy[i]][i] >= 0) sumtemp += v[!obrazy[i]][i]; else sumtemp = 0; sum = max(sumtemp, sum); } if (sum < res) { res = sum; ress = obrazy; } } void BackTrack(int maxx) { if ((int)anna.size() == k) { Check(); return; } for (int i = maxx + 1; i < n; i++) { anna.emplace_back(i); BackTrack(i); anna.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; v.resize(2, vector <ll> (n)); for (int i = 0; i < n; i++) cin >> v[0][i]; for (int i = 0; i < n; i++) cin >> v[1][i]; BackTrack(-1); cout << res << "\n"; for (int i = 0; i < n; i++) { if (ress[i]) cout << 'A'; else cout << 'B'; } cout << "\n"; return 0; }
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 | #include <bits/stdc++.h> using namespace std; #define ll long long vector <int> anna; int n, k; ll res = LLONG_MAX; vector <vector <ll> > v; vector <bool> ress; void Check() { vector <bool> obrazy(n); for (int i = 0; i < k; i++) obrazy[anna[i]] = true; ll sum = 0, sumtemp = 0; for (int i = 0; i < n; i++) { if (sumtemp + v[!obrazy[i]][i] >= 0) sumtemp += v[!obrazy[i]][i]; else sumtemp = 0; sum = max(sumtemp, sum); } if (sum < res) { res = sum; ress = obrazy; } } void BackTrack(int maxx) { if ((int)anna.size() == k) { Check(); return; } for (int i = maxx + 1; i < n; i++) { anna.emplace_back(i); BackTrack(i); anna.pop_back(); } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cin >> n >> k; v.resize(2, vector <ll> (n)); for (int i = 0; i < n; i++) cin >> v[0][i]; for (int i = 0; i < n; i++) cin >> v[1][i]; BackTrack(-1); cout << res << "\n"; for (int i = 0; i < n; i++) { if (ress[i]) cout << 'A'; else cout << 'B'; } cout << "\n"; return 0; } |