結果

問題 No.1029 JJOOII 3
ユーザー HIR180HIR180
提出日時 2020-04-17 21:42:43
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 148 ms / 2,000 ms
コード長 2,717 bytes
コンパイル時間 2,433 ms
コンパイル使用メモリ 223,488 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 13:23:36
合計ジャッジ時間 6,138 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
6,816 KB
testcase_01 AC 4 ms
6,940 KB
testcase_02 AC 6 ms
6,940 KB
testcase_03 AC 18 ms
6,944 KB
testcase_04 AC 16 ms
6,940 KB
testcase_05 AC 67 ms
6,940 KB
testcase_06 AC 70 ms
6,944 KB
testcase_07 AC 65 ms
6,940 KB
testcase_08 AC 69 ms
6,944 KB
testcase_09 AC 65 ms
6,940 KB
testcase_10 AC 66 ms
6,940 KB
testcase_11 AC 71 ms
6,940 KB
testcase_12 AC 64 ms
6,940 KB
testcase_13 AC 64 ms
6,940 KB
testcase_14 AC 68 ms
6,944 KB
testcase_15 AC 65 ms
6,944 KB
testcase_16 AC 63 ms
6,940 KB
testcase_17 AC 63 ms
6,940 KB
testcase_18 AC 64 ms
6,940 KB
testcase_19 AC 7 ms
6,944 KB
testcase_20 AC 48 ms
6,944 KB
testcase_21 AC 46 ms
6,940 KB
testcase_22 AC 48 ms
6,940 KB
testcase_23 AC 45 ms
6,940 KB
testcase_24 AC 48 ms
6,944 KB
testcase_25 AC 51 ms
6,940 KB
testcase_26 AC 66 ms
6,944 KB
testcase_27 AC 76 ms
6,940 KB
testcase_28 AC 70 ms
6,940 KB
testcase_29 AC 66 ms
6,944 KB
testcase_30 AC 75 ms
6,944 KB
testcase_31 AC 70 ms
6,940 KB
testcase_32 AC 147 ms
6,940 KB
testcase_33 AC 147 ms
6,944 KB
testcase_34 AC 146 ms
6,944 KB
testcase_35 AC 147 ms
6,940 KB
testcase_36 AC 148 ms
6,940 KB
testcase_37 AC 5 ms
6,940 KB
testcase_38 AC 7 ms
6,944 KB
testcase_39 AC 6 ms
6,940 KB
testcase_40 AC 6 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

//Let's join Kaede Takagaki Fan Club !!
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
typedef long long ll;
typedef pair<int,int> P;
typedef pair<int,P> P1;
typedef pair<P,P> P2;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-7
#define INF 1000000000
#define fi first
#define sc second
#define rep(i,x) for(int i=0;i<x;i++)
#define repn(i,x) for(int i=1;i<=x;i++)
#define SORT(x) sort(x.begin(),x.end())
#define ERASE(x) x.erase(unique(x.begin(),x.end()),x.end())
#define POSL(x,v) (lower_bound(x.begin(),x.end(),v)-x.begin())
#define POSU(x,v) (upper_bound(x.begin(),x.end(),v)-x.begin())
#define all(x) x.begin(),x.end()
template<class T>
void dmp(T a){
	rep(i,a.size()) cout << a[i] << " ";
	cout << endl;
}
template<class T>
bool chmax(T&a, T b){
	if(a < b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
bool chmin(T&a, T b){
	if(a > b){
		a = b;
		return 1;
	}
	return 0;
}
template<class T>
void g(T &a){
	cin >> a;
}
template<class T>
void o(const T &a,bool space=false){
	cout << a << (space?' ':'\n');
}
//ios::sync_with_stdio(false);
const int mod = 1000000007;
template<class T>
void add(T&a,T b){
	a+=b;
	if(a >= mod) a-=mod;
}


int n, kk;
string s[105];
int c[105];
ll dp[3][100005];
int main(){
	ios::sync_with_stdio(false);
	cin >> n >> kk;
	repn(i, n) cin >> s[i] >> c[i];
	rep(i, 3) rep(j, 100005) dp[i][j] = 1e18;
	string sp = "JOI";
	rep(x, 3){
		dp[x][0] = 0;
		repn(i, n){
			int k = 0;
			for(auto at:s[i]) k += (at == sp[x]);
			rep(j, 100005){
				if(j+k >= 100005) continue;
				chmin(dp[x][j+k], dp[x][j] + c[i]);
			}
		}
	}
	for(int i=100003;i>=0;i--) rep(j, 3) chmin(dp[j][i], dp[j][i+1]);
	//JO & OI & JOI
	vector<P1>vec[2];
	ll ans = 1e18;
	repn(i, n){
		rep(j, s[i].size()+1){
			int a = 0, b = 0;
			rep(k, j) a += (s[i][k] == 'J');
			for(int k=j;k<s[i].size();k++) b += (s[i][k] == 'O');
			vec[0].pb(mp(c[i], mp(a, b)));
			a = 0, b = 0;
			rep(k, j) a += (s[i][k] == 'O');
			for(int k=j;k<s[i].size();k++) b += (s[i][k] == 'I');
			vec[1].pb(mp(c[i], mp(a, b)));
			
			for(int k=j;k<s[i].size()+1;k++) {
				a = 0, b = 0;
				int d = 0;
				rep(aa, j) a += (s[i][aa] == 'J');
				for(int bb=j;bb<k;bb++) b += (s[i][bb] == 'O');
				for(int cc=k;cc<s[i].size();cc++) d += (s[i][cc] == 'I');
				if(b >= kk){
					chmin(ans, 1LL*c[i] + dp[0][max(0, kk-a)] + dp[2][max(0, kk-d)]);
				}
			}
		}
	}
	for(auto at:vec[0]) for(auto at2:vec[1]){
		int z1 = max(0, kk - at.sc.fi);
		int z2 = max(0, kk - at.sc.sc - at2.sc.fi);
		int z3 = max(0, kk - at2.sc.sc);
		chmin(ans, 1LL*at.fi + at2.fi + dp[0][z1] + dp[1][z2] + dp[2][z3]);
	}
	if(ans > 5e17) ans = -1;
	o(ans);
}
0