結果

問題 No.1029 JJOOII 3
ユーザー RhoRho
提出日時 2020-03-27 13:59:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 2,250 bytes
コンパイル時間 1,677 ms
コンパイル使用メモリ 168,228 KB
実行使用メモリ 199,776 KB
最終ジャッジ日時 2024-04-09 19:13:11
合計ジャッジ時間 5,292 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
13,792 KB
testcase_01 AC 2 ms
7,768 KB
testcase_02 AC 4 ms
20,060 KB
testcase_03 AC 14 ms
55,080 KB
testcase_04 AC 21 ms
56,956 KB
testcase_05 AC 107 ms
199,164 KB
testcase_06 AC 92 ms
197,472 KB
testcase_07 AC 97 ms
192,896 KB
testcase_08 AC 102 ms
193,180 KB
testcase_09 AC 93 ms
192,964 KB
testcase_10 AC 95 ms
186,616 KB
testcase_11 AC 107 ms
193,900 KB
testcase_12 AC 81 ms
192,620 KB
testcase_13 AC 104 ms
193,308 KB
testcase_14 AC 107 ms
197,980 KB
testcase_15 AC 87 ms
193,420 KB
testcase_16 AC 93 ms
186,920 KB
testcase_17 AC 97 ms
186,896 KB
testcase_18 AC 98 ms
187,016 KB
testcase_19 AC 5 ms
19,932 KB
testcase_20 AC 39 ms
130,916 KB
testcase_21 AC 37 ms
124,504 KB
testcase_22 AC 40 ms
124,636 KB
testcase_23 AC 37 ms
130,780 KB
testcase_24 AC 38 ms
124,632 KB
testcase_25 AC 42 ms
130,776 KB
testcase_26 AC 56 ms
173,840 KB
testcase_27 AC 64 ms
186,140 KB
testcase_28 AC 60 ms
186,128 KB
testcase_29 AC 55 ms
179,988 KB
testcase_30 AC 62 ms
188,064 KB
testcase_31 AC 60 ms
186,000 KB
testcase_32 AC 169 ms
199,540 KB
testcase_33 AC 169 ms
199,016 KB
testcase_34 AC 168 ms
199,776 KB
testcase_35 AC 168 ms
199,012 KB
testcase_36 AC 170 ms
198,120 KB
testcase_37 AC 3 ms
7,768 KB
testcase_38 AC 3 ms
13,916 KB
testcase_39 AC 3 ms
13,916 KB
testcase_40 AC 2 ms
13,924 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#pragma warning (disable: 4996)
#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
#define MRE assert(0);
const int inf = 1e17;
const int mod = 998244353;
const int maxN = 80;
const int maxK = 100000;
const int maxC = 1000000000;
string s[105];
int c[105], siz[105], cr[105][105][3];
int dp[3][102][100102];
int n, K;

void input() {
	cin >> n >> K;
	assert(1 <= n&&n <= 80);
	assert(1 <= K&&K <= maxK);
	rep(i, n) {
		cin >> s[i] >> c[i];
		assert(0 <= c[i] && c[i] <= maxC);
		assert(1 <= s[i].size() && s[i].size() <= 80);
	}
}

int solve() {
	input();

	rep(i, n) {
		siz[i] = s[i].size();
		rep(j, siz[i]) {
			if (s[i][j] == 'J')cr[i][j + 1][0]++;
			else if (s[i][j] == 'O')cr[i][j + 1][1]++;
			else if (s[i][j] == 'I')cr[i][j + 1][2]++;
			else assert(0);
		}
		rep(j, siz[i]) {
			rep(k, 3) {
				cr[i][j + 1][k] += cr[i][j][k];
			}
		}
	}

	rep(k, 3) {
		rep(i, n + 1)rep(j, K + 100)dp[k][i][j] = inf;
		dp[k][0][0] = 0;
		rep(i, n) {
			rep(j, K + 100) {
				dp[k][i + 1][j] = dp[k][i][j];
				int v = cr[i][siz[i]][k];
				if (j >= v)dp[k][i + 1][j] = min(dp[k][i + 1][j], dp[k][i + 1][j - v] + c[i]);
			}
		}

		for (int j = K + 90; j > 0; j--)dp[k][n][j - 1] = min(dp[k][n][j], dp[k][n][j - 1]);
		//		rep(j, K + 1)cout << dp[k][n][j] << ' '; cout << endl;
	}

	int ans = inf;

	//中央に1個使う

	rep(i, n) {
		if (cr[i][siz[i]][1] < K)continue;
		int res = c[i];
		rep(j, siz[i]) {
			for (int k = j; k < siz[i]; k++) {
				if (cr[i][k + 1][1] - cr[i][j][1] < K)continue;
				int cst = c[i];
				int J = cr[i][j][0], I = cr[i][siz[i]][2] - cr[i][k][2];
				if (J < K)cst += dp[0][n][K - J];
				if (I < K)cst += dp[2][n][K - I];
				ans = min(ans, cst);
			}
		}
	}

	//中央に2個使う

	rep(i, n) {
		rep(j, n) {
			rep(k, siz[i] + 1) {
				rep(l, siz[j] + 1) {//~k-1][k~l-1][l~
					int cst = c[i] + c[j];
					int J = cr[i][k][0], O = cr[i][siz[i]][1] - cr[i][k][1] + cr[j][l][1], I = cr[j][siz[j]][2] - cr[j][l][2];
					if (J < K)cst += dp[0][n][K - J];
					if (O < K)cst += dp[1][n][K - O];
					if (I < K)cst += dp[2][n][K - I];
					ans = min(ans, cst);
				}
			}
		}
	}
	if (ans >= inf)ans = -1;
	return ans;
}


signed main() {
	cout << solve() << endl;
}
0