#include <algorithm> #include <cstdio> #include <string> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define PB push_back #define INT(x) int x; scanf("%d", &x) typedef long long LL; const LL INF = 1000000000000000000LL; int a[100000], b[100000]; int main() { INT(n); INT(k); REP(i,n) scanf("%d", &a[i]); REP(i,n) scanf("%d", &b[i]); int n2 = 1 << n; LL res = INF; string rs; REP(m,n2) if (__builtin_popcount(m) == k) { LL best = 0; REP(i,n) FOR(j,i+1,n+1) { LL loot = 0; FOR(k,i,j) loot += m & (1 << k) ? a[k] : b[k]; best = max(best, loot); } if (best < res) { res = best; rs.clear(); REP(i,n) rs.PB(m & (1 << i) ? 'A' : 'B'); } } printf("%lld\n%s\n", res, rs.c_str()); }
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 | #include <algorithm> #include <cstdio> #include <string> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define PB push_back #define INT(x) int x; scanf("%d", &x) typedef long long LL; const LL INF = 1000000000000000000LL; int a[100000], b[100000]; int main() { INT(n); INT(k); REP(i,n) scanf("%d", &a[i]); REP(i,n) scanf("%d", &b[i]); int n2 = 1 << n; LL res = INF; string rs; REP(m,n2) if (__builtin_popcount(m) == k) { LL best = 0; REP(i,n) FOR(j,i+1,n+1) { LL loot = 0; FOR(k,i,j) loot += m & (1 << k) ? a[k] : b[k]; best = max(best, loot); } if (best < res) { res = best; rs.clear(); REP(i,n) rs.PB(m & (1 << i) ? 'A' : 'B'); } } printf("%lld\n%s\n", res, rs.c_str()); } |