//#pragma GCC optimize("O3") //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef LOCAL #define eprintf(...) fprintf(stderr,__VA_ARGS__) clock_t __my_start_clock = clock(); #else #define eprintf(...) #endif #define TIMESTAMP(x) eprintf("["#x"] Time : %.3lf s.\n", (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC) #define TIMESTAMPf(x,...) eprintf("[" x "] Time : %.3lf s.\n", __VA_ARGS__, (clock() - __my_start_clock)*1.0/CLOCKS_PER_SEC) #define db(x) cerr << #x << " = " << x << endl #define db2(x, y) cerr << "(" << #x << ", " << #y << ") = (" << x << ", " << y << ")\n"; #define db3(x, y, z) cerr << "(" << #x << ", " << #y << ", " << #z << ") = (" << x << ", " << y << ", " << z << ")\n" #define dbv(a) cerr << #a << " = "; for (auto xxxx: a) cerr << xxxx << " "; cerr << endl using namespace std; #ifdef LOCAL static struct __timestamper { string what; __timestamper(const char* what) : what(what){}; __timestamper(const string& what) : what(what){}; ~__timestamper(){ TIMESTAMPf("%s", what.data()); } } __TIMESTAMPER("end"); #else struct __timestamper {}; #endif string solve(string s, vector> a) { int n = s.length(); int half = (n + 1) / 2; int half_mask = (1 << half) - 1; for (int i = 0; i < n; i++) for (int j = 0; j < i; j++) a[i][j] = a[j][i]; vector>> dp(n, vector>(2)); for (int i = 0; i < n; i++) { for (int j = 0; j < 2; j++) { int len = j == 0 ? half : n - half; int start = j == 0 ? 0 : half; dp[i][j].assign(1 << len, 0); for (int mask = 0; mask < (1 << len); mask++) { for (int t = 0; t < len; t++) { if ((mask & (1 << t)) || start + t == i) { dp[i][j][mask] += a[start + t][i]; } } } } } int ans = 0, mans = 0, mask = 0, cost = 0; for (int i = 1; i < (1 << n); i++) { int bit = __builtin_ctz(i); int delta = 0; delta += dp[bit][0][mask & half_mask]; delta += dp[bit][1][mask >> half]; cost += (mask & (1 << bit)) ? -delta : +delta; mask ^= 1 << bit; if (ans < cost) { ans = cost; mans = mask; } } string res; for (int i = 0; i < n; i++) if (mans & (1 << i)) res += s[i]; return res; } vector> gen(mt19937 &rnd, int n) { const int C = 1e6; vector> a(n, vector(n)); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { a[i][j] = rnd() % (C + C + 1) - C; } } return a; } void test() { int n = 30; mt19937 rnd; auto a = gen(rnd, n); solve(string(n, 'a'), a); exit(0); } string slow(string s, vector> a) { int n = s.length(); int ans = -2e9; string sans; for (int mask = 0; mask < (1 << n); mask++) { int cur = 0; for (int i = 0; i < n; i++) if (mask & (1 << i)) { for (int j = i; j < n; j++) if (mask & (1 << j)) { cur += a[i][j]; } } if (ans < cur) { ans = cur; sans = ""; for (int i = 0; i < n; i++) if (mask & (1 << i)) sans += s[i]; } } return sans; } void stress() { for (int it = 0;; it++) { mt19937 rnd(it); if (it % 1000 == 0) db(it); int n = rnd() % 10 + 1; string s; for (int i = 0; i < n; i++) s += 'a' + i; auto a = gen(rnd, n); auto f = [&](string res) { vector h(n); for (int i = 0; i < n; i++) h[i] = std::find(res.begin(), res.end(), s[i]) != res.end(); int score = 0; for (int i = 0; i < n; i++) for (int j = i; j < n; j++) if (h[i] && h[j]) score += a[i][j]; return score; }; auto ans1 = solve(s, a); auto ans2 = slow(s, a); if (f(ans1) != f(ans2)) { db(it); db(s); for (auto v : a) { dbv(v); } db2(ans1, ans2); db2(f(ans1), f(ans2)); exit(0); } } } int main() { #ifdef LOCAL freopen("in", "r", stdin); test(); // stress(); #endif string s; cin >> s; int n = s.length(); vector> a(n, vector(n)); for (int i = 0; i < n; i++) { for (int j = i; j < n; j++) { cin >> a[i][j]; } } auto ans = solve(s, a); cout << ans.length() << endl << ans << endl; return 0; }