#include<iostream> #include<cstdio> #include<cstring> #define MAX_K 8 #define MAX_N 300 usingnamespace std; typedeflonglong lnt; char str[MAX_N+5]; int n, m, s[MAX_N+5], c[1<<MAX_K]; lnt w[1<<MAX_K], f[MAX_N+5][MAX_N+5][1<<MAX_K]; voidupd(lnt &a, lnt b){if (a < b) a = b;} intmain(){ scanf("%d%d%s", &n, &m, str), memset(f, -1, sizeof(f)); for (int i = 0; i < (1<<m); i++) scanf("%d%lld", c+i, w+i); for (int i = 1; i <= n; i++) s[i] = str[i-1]-'0', f[i][i][s[i]] = 0; for (int l = 2; l <= n; l++) for (int s = 1, t = s+l-1; t <= n; t = ++s+l-1) { int tar = l-1; while (tar >= m) tar -= m-1; for (int mid = t-1; mid >= s; mid -= m-1) for (int sta = 0; sta < (1<<tar); sta++) { if (~f[s][mid][sta] && ~f[mid+1][t][0]) upd(f[s][t][sta<<1], f[s][mid][sta]+f[mid+1][t][0]); if (~f[s][mid][sta] && ~f[mid+1][t][1]) upd(f[s][t][sta<<1|1], f[s][mid][sta]+f[mid+1][t][1]); } if (tar == m-1) { lnt g[2]; g[0] = g[1] = -1; for (int sta = 0; sta < (1<<m); sta++) if (~f[s][t][sta]) upd(g[c[sta]], f[s][t][sta]+w[sta]); f[s][t][0] = g[0], f[s][t][1] = g[1]; } } lnt ans = 0; for (int i = 0; i < (1<<m); i++) ans = max(ans, f[1][n][i]); printf("%lld", ans); return0; }