package main import ( "bufio" "fmt" "os" "sort" "strconv" "strings" ) var sc = bufio.NewScanner(os.Stdin) var wr = bufio.NewWriter(os.Stdout) func out(x ...interface{}) { fmt.Fprintln(wr, x...) } func getI() int { sc.Scan() i, e := strconv.Atoi(sc.Text()) if e != nil { panic(e) } return i } func getF() float64 { sc.Scan() i, e := strconv.ParseFloat(sc.Text(), 64) if e != nil { panic(e) } return i } func getInts(N int) []int { ret := make([]int, N) for i := 0; i < N; i++ { ret[i] = getI() } return ret } func getS() string { sc.Scan() return sc.Text() } // min, max, asub, absなど基本関数 func max(a, b int) int { if a > b { return a } return b } func min(a, b int) int { if a < b { return a } return b } func asub(a, b int) int { if a > b { return a - b } return b - a } func abs(a int) int { if a >= 0 { return a } return -a } func lowerBound(a []int, x int) int { idx := sort.Search(len(a), func(i int) bool { return a[i] >= x }) return idx } func upperBound(a []int, x int) int { idx := sort.Search(len(a), func(i int) bool { return a[i] > x }) return idx } const inf = int(1e18) func main() { defer wr.Flush() sc.Split(bufio.ScanWords) sc.Buffer([]byte{}, 1000000) // this template is new version. // use getI(), getS(), getInts(), getF() N, K := getI(), getI() s := make([]string, N) c := make([]int, N+1) for i := 0; i < N; i++ { s[i] = getS() c[i] = getI() } // DP0 dp0 := make([][]int, N+1) for i := 0; i <= N; i++ { dp0[i] = make([]int, 110000) } for j := 0; j < 110000; j++ { dp0[0][j] = inf } dp0[0][0] = 0 for i := 1; i <= N; i++ { n := strings.Count(s[i-1], "J") for j := 0; j < 110000; j++ { dp0[i][j] = dp0[i-1][j] if j-n >= 0 { dp0[i][j] = min(dp0[i-1][j], dp0[i][j-n]+c[i-1]) } } } // DP1 dp1 := make([][]int, N+1) for i := 0; i <= N; i++ { dp1[i] = make([]int, 110000) } for j := 0; j < 110000; j++ { dp1[0][j] = inf } dp1[0][0] = 0 for i := 1; i <= N; i++ { n := strings.Count(s[i-1], "O") for j := 0; j < 110000; j++ { dp1[i][j] = dp1[i-1][j] if j-n >= 0 { dp1[i][j] = min(dp1[i-1][j], dp1[i][j-n]+c[i-1]) } } } // DP2 dp2 := make([][]int, N+1) for i := 0; i <= N; i++ { dp2[i] = make([]int, 110000) } for j := 0; j < 110000; j++ { dp2[0][j] = inf } dp2[0][0] = 0 for i := 1; i <= N; i++ { n := strings.Count(s[i-1], "I") for j := 0; j < 110000; j++ { dp2[i][j] = dp2[i-1][j] if j-n >= 0 { dp2[i][j] = min(dp2[i-1][j], dp2[i][j-n]+c[i-1]) } } } J := make([]int, N) O := make([]int, N) I := make([]int, N) for i := 0; i < N; i++ { J[i] = strings.Count(s[i], "J") O[i] = strings.Count(s[i], "O") I[i] = strings.Count(s[i], "I") } // ここに記述;N文字目でJ,O,Iがいくつ出現しているかの累積を計算しておく s = append(s, "") c = append(c, 0) // out(dp0[N][:10]) // out(dp1[N][:10]) // out(dp2[N][:10]) ans := inf for i := 0; i <= N; i++ { for j := 0; j <= N; j++ { j0 := 0 o0 := 0 if i != N { o0 = O[i] } for k := 0; k <= len(s[i]); k++ { o1 := 0 i1 := 0 if j != N { i1 = I[j] } for l := 0; l <= len(s[j]); l++ { numJ := max(0, K-j0) numO := max(0, K-o0-o1) numI := max(0, K-i1) cost := c[i] + c[j] + dp0[N][numJ] + dp1[N][numO] + dp2[N][numI] ans = min(ans, cost) // out(s[i][0:k], s[i][k:len(s[i])], j0, o0, // ":", // s[j][0:l], s[j][l:len(s[j])], o1, i1, // "JOI", numJ, numO, numI, "DP", // dp0[N][numJ], dp1[N][numO], dp2[N][numI], // "C", c[i], c[j], // cost) if l != len(s[j]) { if s[j][l] == 'O' { o1++ } if s[j][l] == 'I' { i1-- } } } if k != len(s[i]) { if s[i][k] == 'J' { j0++ } if s[i][k] == 'O' { o0-- } } } } } if ans == inf { out(-1) return } out(ans) }