public int minCost(String s, int[] cost, int k) {
int n = s.length();
final int CCC = 100;
final int L = 6;
final int INF = 1000 * 1000 * 1000 + 500;
int[][][][] with = new int[2][k+2][n+2][L];
int[][][][] without = new int[2][k+2][n+2][L];
for(int a = 0; a < 2; ++a)
for(int b = 0; b <= k; ++b)
for(int c = 0; c <= n; ++c)
for(int d = 0; d < L; ++d)
with[a][b][c][d] = without[a][b][c][d] = INF;
for(int d = 0; d < L; ++d)
with[0][0][0][d] = without[0][0][0][d] = 0;
for(int position = 0; position < n; ++position) {
int letter = s.charAt(position) - 'a';
for(int b = 0; b <= k; ++b)
for(int c = 0; c <= n; ++c)
for(int d = 0; d < L; ++d) {
with[1][b][c][d] = without[1][b][c][d] = INF;
for(int d2 = 0; d2 < L; ++d2) {
if(d != d2 || c == 0)
without[0][b][c][d2] = min(without[0][b][c][d2], with[0][b][c][d]);
if(c == 0)
with[0][b][c][d2] = min(with[0][b][c][d2], without[0][b][c][d]);
}
}
for(int b = 0; b <= k; ++b)
for(int c = 0; c <= n; ++c)
for(int d = 0; d < L; ++d) {
int old = with[0][b][c][d];
if(d == letter)
with[1][b][c+1][d] = min(with[1][b][c+1][d], old - CCC*position + cost[position]);
if(d != letter && c-1 >= 0)
with[1][b][c-1][d] = min(with[1][b][c-1][d], old + CCC*position + cost[position]);
with[1][b+1][c][d] = min(with[1][b+1][c][d], old); // not taking it
old = without[0][b][c][d];
if(d == letter && c-1 >= 0)
without[1][b][c-1][d] = min(without[1][b][c-1][d], old + CCC*position + cost[position]);
if(d != letter)
without[1][b][c+1][d] = min(without[1][b][c+1][d], old - CCC*position + cost[position]);
without[1][b+1][c][d] = min(without[1][b+1][c][d], old); // not taking it
}
for(int b = 0; b <= k; ++b)
for(int c = 0; c <= n; ++c)
for(int d = 0; d < L; ++d) {
with[0][b][c][d] = with[1][b][c][d];
without[0][b][c][d] = without[1][b][c][d];
}
}
int ans = INF;
for(int b = 0; b <= k; ++b)
for(int d = 0; d < L; ++d) {
ans = min(ans, with[0][b][0][d]);
ans = min(ans, without[0][b][0][d]);
}
if(ans == INF) ans = -1;
return ans;
}