結果

問題 No.1029 JJOOII 3
ユーザー pekempeypekempey
提出日時 2020-04-17 22:39:09
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 273 ms / 2,000 ms
コード長 1,319 bytes
コンパイル時間 1,666 ms
コンパイル使用メモリ 170,096 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 14:57:00
合計ジャッジ時間 7,076 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 16 ms
6,944 KB
testcase_04 AC 53 ms
6,940 KB
testcase_05 AC 245 ms
6,944 KB
testcase_06 AC 169 ms
6,944 KB
testcase_07 AC 216 ms
6,944 KB
testcase_08 AC 228 ms
6,944 KB
testcase_09 AC 197 ms
6,940 KB
testcase_10 AC 212 ms
6,940 KB
testcase_11 AC 256 ms
6,944 KB
testcase_12 AC 143 ms
6,940 KB
testcase_13 AC 263 ms
6,940 KB
testcase_14 AC 265 ms
6,940 KB
testcase_15 AC 59 ms
6,944 KB
testcase_16 AC 3 ms
6,944 KB
testcase_17 AC 157 ms
6,944 KB
testcase_18 AC 241 ms
6,940 KB
testcase_19 AC 2 ms
6,940 KB
testcase_20 AC 3 ms
6,944 KB
testcase_21 AC 3 ms
6,940 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 3 ms
6,940 KB
testcase_24 AC 3 ms
6,940 KB
testcase_25 AC 3 ms
6,944 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 3 ms
6,944 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 3 ms
6,940 KB
testcase_30 AC 3 ms
6,944 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 273 ms
6,944 KB
testcase_33 AC 273 ms
6,944 KB
testcase_34 AC 272 ms
6,940 KB
testcase_35 AC 272 ms
6,944 KB
testcase_36 AC 273 ms
6,940 KB
testcase_37 AC 2 ms
6,940 KB
testcase_38 AC 2 ms
6,940 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#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;
  }
}
0