题目
Luogu
LOJ
Acwing
思路
代码
#include <iostream>
#include <cstring>
#include <algorithm>
#define int long long
using namespace std;
const int N = 310, S = 1 << 8, INF = 1e18;
int n, k, c[N], w[N], a[N], f[N][N][S], g[2];
string str;
signed main() {
cin >> n >> k >> str;
for (int i = 0; i < str.size(); i++)
a[i + 1] = str[i] - '0';
// 注意洛谷的读入方式略有不同
// for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 0; i < (1 << k); i++) cin >> c[i] >> w[i];
memset(f, -0x3f, sizeof f);
for (int i = 1; i <= n; i++) f[i][i][a[i]] = 0;
for (int len = 2; len <= n; len++) {
for (int i = 1; i + len - 1 <= n; i++) {
int j = i + len - 1, x = (len - 1) % (k - 1);
if (x == 0) x = k - 1;
for (int mid = j - 1; mid >= i; mid -= k - 1)
for (int s = 0; s < (1 << x); s++)
f[i][j][s << 1] = max(f[i][j][s << 1], f[i][mid][s] + f[mid + 1][j][0]),
f[i][j][s << 1 | 1] = max(f[i][j][s << 1 | 1], f[i][mid][s] + f[mid + 1][j][1]);
if (x == k - 1) {
g[0] = g[1] = -INF;
for (int s = 0; s < (1 << k); s++)
g[c[s]] = max(g[c[s]], f[i][j][s] + w[s]);
f[i][j][0] = g[0], f[i][j][1] = g[1];
}
}
}
int res = -INF;
for (int s = 0; s < (1 << k); s++)
res = max(res, f[1][n][s]);
cout << res << endl;
return 0;
}