#define INF 0x7f7f7f7f #define MAXN 100005 #define N 200005 #define P 2 #define MOD 99991
typedeflonglong ll;
namespace fastIO { //char buf[(1 << 22)], *p1 = buf, *p2 = buf; inlineint(){ char c = getchar(); int x = 0, f = 1; while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } }
usingnamespace fastIO; usingnamespacestd;
int t, n, m, a[300], dp[2005][2004]; string s; intmain(){ while (cin >> n >> m) { cin >> s; int x, y; char t; for (int i = 1; i <= n; i++) { cin >> t >> x >> y; a[t] = min(x, y); } for (int len = 2; len <= m; len++) for (int i = 1; i + len - 1 <= m; i++) { int j = i + len - 1; dp[i][j] = INF; if (s[i - 1] == s[j - 1]) dp[i][j] = dp[i + 1][j - 1]; else { dp[i][j] = min(dp[i][j], dp[i + 1][j] + a[s[i - 1]]); dp[i][j] = min(dp[i][j], dp[i][j - 1] + a[s[j - 1]]); } } cout << dp[1][m] << endl; } }
近期评论