#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
typedef long long ll;
int n, k, a[305], c[305];
ll w[305], dp[305][305][265], g[2];
int main(){
memset(dp, 0xcf, sizeof(dp));
cin>>n>>k;
for(int i=1; i<=n; i++)
scanf("%1d", &a[i]);
for(int i=0; i<(1<<k); i++)
scanf("%d %lld", &c[i], &w[i]);
for(int i=n; i; i--)
for(int j=i; j<=n; j++){
if(i==j){
dp[i][j][a[i]] = 0;
continue;
}
int len=j-i;
len %= k - 1;
if(!len) len = k - 1;
for(int mid=j; mid>i; mid-=k-1)
for(int op=0; op<(1<<len); op++){
dp[i][j][op<<1] = max(dp[i][j][op<<1], dp[i][mid-1][op]+dp[mid][j][0]);
dp[i][j][(op<<1)|1] = max(dp[i][j][(op<<1)|1], dp[i][mid-1][op]+dp[mid][j][1]);
}
if(len==k-1){
g[0] = g[1] = -0x3f3f3f3f3f3f3f3f;
for(int o=0; o<(1<<k); o++)
g[c[o]] = max(g[c[o]], dp[i][j][o]+w[o]);
dp[i][j][0] = g[0];
dp[i][j][1] = g[1];
}
}
ll ans=-0x3f3f3f3f3f3f3f3f;
for(int i=0; i<(1<<k); i++)
ans = max(ans, dp[1][n][i]);
cout<<ans<<endl;
return 0;
}
转载于:https://www.cnblogs.com/poorpool/p/9004838.html