結果
問題 | No.1029 JJOOII 3 |
ユーザー |
|
提出日時 | 2020-04-17 22:39:09 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 254 ms / 2,000 ms |
コード長 | 1,319 bytes |
コンパイル時間 | 1,504 ms |
コンパイル使用メモリ | 175,360 KB |
実行使用メモリ | 5,760 KB |
最終ジャッジ日時 | 2024-10-03 14:19:24 |
合計ジャッジ時間 | 6,191 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 38 |
ソースコード
#include <bits/stdc++.h> using namespace std; using ll = long long; #define rep(i, n) for (int i = 0; i < (n); i++) #define repr(i, n) for (int i = (n) - 1; i >= 0; i--) #define range(a) a.begin(), a.end() int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N, K; cin >> N >> K; vector<ll> dp(3 * K + 1, LLONG_MAX); dp[0] = 0; vector<string> S(N); vector<ll> C(N); rep(i, N) cin >> S[i] >> C[i]; vector<int> J(N); vector<int> O(N); vector<int> I(N); rep(i, N) { J[i] = count(range(S[i]), 'J'); O[i] = count(range(S[i]), 'O'); I[i] = count(range(S[i]), 'I'); } rep(i, 3 * K) if (dp[i] != LLONG_MAX) { rep(j, N) { int l = i; if (l / K == (l + S[j].size()) / K) { if (l < K) { l += J[j]; } else if (l < 2 * K) { l += O[j]; } else { l += I[j]; } l = min(l, 3 * K); } else { for (char c : S[j]) { if (l < K) { if (c == 'J') l++; } else if (l < 2 * K) { if (c == 'O') l++; } else if (l < 3 * K) { if (c == 'I') l++; } } } dp[l] = min(dp[l], dp[i] + C[j]); } } if (dp[3 * K] == LLONG_MAX) { cout << -1 << endl; } else { cout << dp[3 * K] << endl; } }