#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #define endl codeforces #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair; using tii = std::tuple; using pll = std::pair; using tll = std::tuple; template using vec = std::vector; template using vvec = vec>; template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } template const T& clamp(const T &t, const T &low, const T &high) { return std::max(low, std::min(high, t)); } template void chclamp(T &t, const T &low, const T &high) { return t = clamp(t, low, high); } template T make_v(T init) { return init; } template auto make_v(T init, std::size_t s, Tail... tail) { auto v = std::move(make_v(init, tail...)); return vec(s, v); } template struct multi_dem_array { using type = std::array::type, Head>; }; template struct multi_dem_array { using type = std::array; }; template using mdarray = typename multi_dem_array::type; namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } constexpr std::size_t SIZE = 80; constexpr ll inf = 5e15; struct Solver { ll n, k; vec> sv; vvec cnts, lcost; mdarray minc; mdarray substr; Solver(ll n, ll k) : n(n), k(k), sv(n), cnts(make_v(0, 3, n)), lcost(make_v(inf, 3, k + 1)) { for (ll i = 0; i < 2; i++) for (auto &&v : minc[i]) std::fill(ALL(v), inf); minc[0][0][0] = minc[1][0][0] = 0; for (ll i = 1; i < SIZE; i++) for (ll j = 1; j < SIZE; j++) { substr[0][i][j] = std::string(i, 'J') + std::string(j, 'O'); substr[1][i][j] = std::string(i, 'O') + std::string(j, 'I'); } ll idx = 0; char cs[] = { 'J', 'O', 'I', }; for (auto &&e : sv) { std::string s; ll c; std::cin >> s >> c; e = std::make_pair(s, c); for (ll i = 1; i < SIZE; i++) for (ll j = 1; j < SIZE; j++) { if (in_s(s, substr[0][i][j])) chmin(minc[0][i][j], c); if (in_s(s, substr[1][i][j])) chmin(minc[1][i][j], c); } for (ll i = 0; i < 3; i++) { ll cnt = 0; for (char c : s) if (c == cs[i]) cnt++; cnts[i][idx] = cnt; } idx++; } } bool in_s(const std::string &a, const std::string &b) { ll idx = 0; for (char c : a) { if (c == b[idx]) idx++; if (idx == b.size()) break; } return idx == b.size(); } void calc_lc(ll cidx) { lcost[cidx][0] = 0; for (ll i = 0; i < n; i++) { ll cnt = cnts[cidx][i]; ll cost = sv[i].second; if (cnt == 0) continue; while (true) { for (ll j = k; 0 <= j; j--) { ll pre = std::max(j - cnt, 0); chmin(lcost[cidx][j], lcost[cidx][pre] + cost); } cnt *= 2; cost *= 2; if (k <= cnt) break; } } } ll solve() { for (ll i = 0; i < 3; i++) calc_lc(i); ll ans = inf; for (ll i = 0; i <= k; i++) for (ll j = 0; j <= k; j++) for (ll l = 0; l <= k; l++) for (ll m = 0; m <= k; m++) { ll tmp = 0; tmp += minc[0][i][j]; tmp += minc[1][l][m]; ll rest_j = k - i; ll rest_o = std::max(k - (j + l), 0); ll rest_i = k - m; tmp += lcost[0][rest_j] + lcost[1][rest_o] + lcost[2][rest_i]; chmin(ans, tmp); } return ans == inf ? -1 : ans; } }; int main() { ll n, k; std::cin >> n >> k; Solver solver(n, k); std::cout << solver.solve() << '\n'; return 0; }