#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned int ui; const ll mod = 1000000007; const ll INF = (ll)1000000007 * 1000000007; typedef pair P; #define stop char nyaa;cin>>nyaa; #define rep(i,n) for(int i=0;i=0;i--) #define Rep(i,sta,n) for(int i=sta;i=sta;i--) #define rep1(i,n) for(int i=1;i<=n;i++) #define per1(i,n) for(int i=n;i>=1;i--) #define Rep1(i,sta,n) for(int i=sta;i<=n;i++) typedef long double ld; const ld eps = 1e-8; const ld pi = acos(-1.0); typedef pair LP; int dx[4]={1,-1,0,0}; int dy[4]={0,0,1,-1}; int n,k; ll c[85]; vector s[85]; int cnt[85][3]; ll mono[3][100010]; ll dp[3][85][100010]; void solve(){ cin >> n >> k; rep(i,n){ string s_; cin >> s_ >> c[i]; s[i].resize(s_.size()); rep(j,s_.size()){ if(s_[j]=='J'){ s[i][j]=0; cnt[i][0]+=1; } if(s_[j]=='O'){ s[i][j]=1; cnt[i][1]+=1; } if(s_[j]=='I'){ s[i][j]=2; cnt[i][2]+=1; } } } // rep(i,n){ // rep(p,3){ // cout << cnt[i][p] << " "; // } // cout << "" << endl; //} rep(p,3){ rep(i,n+1){ rep(j,k+1){ dp[p][i][j]=INF; } } } rep(p,3){ dp[p][0][0]=0; rep(i,n){ rep(j,k+1){ if(j-cnt[i][p]>=0)dp[p][i+1][j]=min(dp[p][i][j],dp[p][i+1][j-cnt[i][p]]+c[i]); else if(j==k) dp[p][i+1][j]=min(dp[p][i][j],dp[p][i+1][0]+c[i]); else dp[p][i+1][j]=dp[p][i][j]; } } } // rep(p,3){ // rep(i,n+1){ // rep(j,k+1){ // cout << p << " " << i << " " << j << " " << dp[p][i][j] << endl; // } // } // } rep(p,3){ rep(j,k+2){ mono[p][j]=INF; } } rep(p,3){ per(j,k+1){ mono[p][j]=min(mono[p][j+1],dp[p][n][j]); } } // rep(p,3){ // rep(j,k+1){ // cout << p << " " << j << " " << mono[p][j] << endl; // } // } ll ans=INF; rep(i1,n){ int J_num=0; rep(t1,s[i1].size()){ if(s[i1][t1]!=0) continue; J_num+=1; int O_num1=0; Per(t2,t1+1,s[i1].size()){ if(s[i1][t2]!=1) continue; O_num1+=1; rep(i2,n){ int O_num2=0; rep(t3,s[i2].size()){ if(s[i2][t3]!=1) continue; O_num2+=1; int I_num=0; Per(t4,t3+1,s[i2].size()){ if(s[i2][t4]!=2) continue; I_num+=1; ans=min(ans,c[i1]+c[i2]+mono[0][max(0,k-J_num)]+mono[1][max(0,k-O_num1-O_num2)]+mono[2][max(0,k-I_num)]); } } } } } } rep(i,n){ int J_num=0; rep(t1,s[i].size()){ if(s[i][t1]!=0) continue; J_num+=1; int I_num=0; Per(t2,t1+1,s[i].size()){ if(s[i][t2]!=2) continue; I_num+=1; int O_num=0; Rep(t3,t1+1,t2){ if(s[i][t3]==1) O_num+=1; } if(O_num>=k) ans=min(ans,c[i]+mono[0][max(0,k-J_num)]+mono[2][max(0,k-I_num)]); } } } if(ans