// C++ program to print all combinations of size // k of elements in set 1..n #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = a; i <= b; i++) void makeCombiUtil(vector<vector<int> >& ans, vector<int>& tmp, int n, int left, int k) { if (k == 0) { ans.push_back(tmp); return; } for (int i = left; i <= n; ++i) { tmp.push_back(i); makeCombiUtil(ans, tmp, n, i + 1, k - 1); tmp.pop_back(); } } vector<vector<int> > makeCombi(int n, int k) { vector<vector<int> > ans; vector<int> tmp; makeCombiUtil(ans, tmp, n, 1, k); return ans; } const int N_MAX = 100123; typedef long long ll; ll a [ N_MAX ]; ll b [ N_MAX ]; ll c [ N_MAX ]; // given number int n = 5; int k = 3; ll countMaxSubsequence() { ll ans = 0; ll localAns = 0; REP(i,1,n) { if(localAns+c[i]>0) { localAns+=c[i]; ans = max(localAns,ans); } else { localAns = 0; } } return ans; } ll wynik = LLONG_MAX; char example [ N_MAX ]; int resIndex = -1; int main() { ios_base::sync_with_stdio(0); cin >> n >> k; REP(i,1,n) { cin >> a[i]; } REP(i,1,n) { cin >> b[i]; } vector<vector<int> > ans = makeCombi(n, k); for (int i = 0; i < (int) ans.size(); i++) { REP(i,1,n) { c[i] = b[i]; } for (int j = 0; j < (int) ans[i].size(); j++) { int index = ans[i][j]; c[index] = a[index]; //cout << "TU" << "\n"; } ll maxsubSequence = countMaxSubsequence(); if(maxsubSequence<wynik) { wynik = maxsubSequence; resIndex = i; } } //utworz przyklad REP(i,1,n) { example[i] = 'B'; } for(auto index : ans[resIndex]) { example[index] = 'A'; } cout << wynik << '\n'; REP(i,1,n) { cout << example[i]; } cout << '\n'; return 0; } /* 6 2 -1 7 0 2 -5 0 3 1 4 -3 -3 12 */
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 | // C++ program to print all combinations of size // k of elements in set 1..n #include <bits/stdc++.h> using namespace std; #define REP(i,a,b) for (int i = a; i <= b; i++) void makeCombiUtil(vector<vector<int> >& ans, vector<int>& tmp, int n, int left, int k) { if (k == 0) { ans.push_back(tmp); return; } for (int i = left; i <= n; ++i) { tmp.push_back(i); makeCombiUtil(ans, tmp, n, i + 1, k - 1); tmp.pop_back(); } } vector<vector<int> > makeCombi(int n, int k) { vector<vector<int> > ans; vector<int> tmp; makeCombiUtil(ans, tmp, n, 1, k); return ans; } const int N_MAX = 100123; typedef long long ll; ll a [ N_MAX ]; ll b [ N_MAX ]; ll c [ N_MAX ]; // given number int n = 5; int k = 3; ll countMaxSubsequence() { ll ans = 0; ll localAns = 0; REP(i,1,n) { if(localAns+c[i]>0) { localAns+=c[i]; ans = max(localAns,ans); } else { localAns = 0; } } return ans; } ll wynik = LLONG_MAX; char example [ N_MAX ]; int resIndex = -1; int main() { ios_base::sync_with_stdio(0); cin >> n >> k; REP(i,1,n) { cin >> a[i]; } REP(i,1,n) { cin >> b[i]; } vector<vector<int> > ans = makeCombi(n, k); for (int i = 0; i < (int) ans.size(); i++) { REP(i,1,n) { c[i] = b[i]; } for (int j = 0; j < (int) ans[i].size(); j++) { int index = ans[i][j]; c[index] = a[index]; //cout << "TU" << "\n"; } ll maxsubSequence = countMaxSubsequence(); if(maxsubSequence<wynik) { wynik = maxsubSequence; resIndex = i; } } //utworz przyklad REP(i,1,n) { example[i] = 'B'; } for(auto index : ans[resIndex]) { example[index] = 'A'; } cout << wynik << '\n'; REP(i,1,n) { cout << example[i]; } cout << '\n'; return 0; } /* 6 2 -1 7 0 2 -5 0 3 1 4 -3 -3 12 */ |