結果

問題 No.1029 JJOOII 3
ユーザー chocopuuchocopuu
提出日時 2020-04-17 22:33:56
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,100 bytes
コンパイル時間 2,299 ms
コンパイル使用メモリ 204,460 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 14:51:52
合計ジャッジ時間 4,108 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,812 KB
testcase_01 AC 2 ms
6,940 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 4 ms
6,940 KB
testcase_04 AC 7 ms
6,940 KB
testcase_05 AC 25 ms
6,940 KB
testcase_06 AC 18 ms
6,940 KB
testcase_07 AC 22 ms
6,940 KB
testcase_08 AC 24 ms
6,944 KB
testcase_09 AC 21 ms
6,940 KB
testcase_10 AC 21 ms
6,940 KB
testcase_11 AC 27 ms
6,944 KB
testcase_12 AC 15 ms
6,944 KB
testcase_13 AC 27 ms
6,944 KB
testcase_14 AC 27 ms
6,940 KB
testcase_15 AC 19 ms
6,944 KB
testcase_16 AC 22 ms
6,940 KB
testcase_17 AC 25 ms
6,940 KB
testcase_18 AC 25 ms
6,940 KB
testcase_19 WA -
testcase_20 AC 2 ms
6,944 KB
testcase_21 AC 2 ms
6,940 KB
testcase_22 AC 2 ms
6,944 KB
testcase_23 AC 2 ms
6,940 KB
testcase_24 AC 2 ms
6,944 KB
testcase_25 AC 2 ms
6,944 KB
testcase_26 AC 2 ms
6,940 KB
testcase_27 AC 2 ms
6,940 KB
testcase_28 AC 2 ms
6,940 KB
testcase_29 AC 2 ms
6,944 KB
testcase_30 AC 2 ms
6,940 KB
testcase_31 AC 2 ms
6,940 KB
testcase_32 AC 29 ms
6,940 KB
testcase_33 AC 29 ms
6,944 KB
testcase_34 AC 29 ms
6,940 KB
testcase_35 AC 29 ms
6,940 KB
testcase_36 AC 29 ms
6,940 KB
testcase_37 WA -
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,944 KB
testcase_40 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define int long long
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define RREP(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define FOR(i, s, n) for (int i = s; i < (int)n; ++i)
#define RFOR(i, s, n) for (int i = (int)n - 1; i >= s; --i)
#define ALL(a) a.begin(), a.end()
#define IN(a, x, b) (a <= x && x < b)
template<class T>inline void out(T t){cout << t << "\n";}
template<class T,class... Ts>inline void out(T t,Ts... ts){cout << t << " ";out(ts...);}
template<class T>inline bool CHMIN(T&a,T b){if(a > b){a = b;return true;}return false;}
template<class T>inline bool CHMAX(T&a,T b){if(a < b){a = b;return true;}return false;}
constexpr int INF = 1e18;



signed main(){
	int N,K;
	cin >> N >> K;
	vector<tuple<string,int>>v(N);
	REP(i,N){
		string s;
		int c;
		cin >> s >> c;
		v[i] = {s,c};
	}
	vector<int>dp1(K + 100,INF);
	dp1[0] = 0;
	REP(i,N){
		auto[s,c] = v[i];
		int cnt = 0;
		REP(j,s.size()){
			if(s[j] == 'J')++cnt;
		}
		REP(j,K){
			CHMIN(dp1[j + cnt],dp1[j] + c);
		}
	}
	RREP(i,K + 99)CHMIN(dp1[i],dp1[i + 1]);
	vector<int>dp2(K + 100,INF);
	dp2[0] = dp1[K];
	REP(i,N){
		int jj = 0,o = 0;
		auto[s,c] = v[i];
		REP(j,s.size()){
			if(s[j] == 'O')++o;
		}
		REP(j,s.size()){
			if(s[j] == 'J')++jj;
			else if(s[j] == 'O')--o;
			if(K - jj >= 0)CHMIN(dp2[o],dp1[K - jj] + c);
		}
	}
	REP(i,N){
		auto[s,c] = v[i];
		int cnt = 0;
		REP(j,s.size()){
			if(s[j] == 'O')++cnt;
		}
		REP(j,K){
			CHMIN(dp2[j + cnt],dp2[j] + c);
		}
	}
	RREP(i,K + 99)CHMIN(dp2[i],dp2[i + 1]);
	vector<int>dp3(K + 100,INF);
	dp3[0] = dp2[K];
	REP(i,N){
		int o = 0,ii = 0;
		auto[s,c] = v[i];
		REP(j,s.size()){
			if(s[j] == 'I')++ii;
		}
		REP(j,s.size()){
			if(s[j] == 'O')++o;
			else if(s[j] == 'I')--ii;
			if(K - o >= 0)CHMIN(dp3[ii],dp2[K - o] + c);
		}
	}
	REP(i,N){
		auto[s,c] = v[i];
		int cnt = 0;
		REP(j,s.size()){
			if(s[j] == 'I')++cnt;
		}
		REP(j,K){
			CHMIN(dp3[j + cnt],dp3[j] + c);
		}
	}
	RREP(i,K + 99)CHMIN(dp3[i],dp3[i + 1]);
	if(dp3[K] == INF)out(-1);
	else out(dp3[K]);
	//REP(i,K + 1)out(i,dp1[i],dp2[i],dp3[i]);
}
0