結果

問題 No.1029 JJOOII 3
ユーザー carrot46carrot46
提出日時 2020-04-17 22:27:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 2,320 bytes
コンパイル時間 2,017 ms
コンパイル使用メモリ 173,708 KB
実行使用メモリ 258,176 KB
最終ジャッジ日時 2024-04-14 14:44:07
合計ジャッジ時間 7,329 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 1 ms
6,944 KB
testcase_02 AC 2 ms
6,944 KB
testcase_03 AC 15 ms
15,872 KB
testcase_04 AC 47 ms
50,176 KB
testcase_05 AC 213 ms
225,664 KB
testcase_06 AC 153 ms
160,640 KB
testcase_07 AC 188 ms
198,528 KB
testcase_08 AC 200 ms
211,456 KB
testcase_09 AC 176 ms
184,704 KB
testcase_10 AC 181 ms
192,512 KB
testcase_11 AC 229 ms
240,512 KB
testcase_12 AC 129 ms
132,352 KB
testcase_13 AC 230 ms
243,072 KB
testcase_14 AC 235 ms
246,272 KB
testcase_15 AC 162 ms
165,120 KB
testcase_16 AC 187 ms
191,232 KB
testcase_17 AC 216 ms
224,640 KB
testcase_18 AC 220 ms
228,224 KB
testcase_19 WA -
testcase_20 AC 4 ms
6,940 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 4 ms
6,940 KB
testcase_23 AC 4 ms
6,944 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 4 ms
6,940 KB
testcase_26 AC 4 ms
6,940 KB
testcase_27 AC 5 ms
6,940 KB
testcase_28 AC 4 ms
6,940 KB
testcase_29 AC 4 ms
6,940 KB
testcase_30 AC 5 ms
6,944 KB
testcase_31 AC 4 ms
6,940 KB
testcase_32 AC 248 ms
258,176 KB
testcase_33 AC 248 ms
258,048 KB
testcase_34 AC 249 ms
258,048 KB
testcase_35 AC 246 ms
258,048 KB
testcase_36 AC 248 ms
258,176 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>
#include <iomanip>
using namespace std;
#define reps(i,s,n) for(int i = s; i < n; i++)
#define rep(i,n) reps(i,0,n)
#define Rreps(i,n,e) for(int i = n - 1; i >= e; --i)
#define Rrep(i,n) Rreps(i,n,0)
#define ALL(a) a.begin(), a.end()
#define fi first
#define se second
typedef long long ll;
typedef vector<ll> vec;
typedef vector<vec> mat;

ll N,M,H,W,K,Q,A,B;
string S;
const ll MOD = 998244353;
//const ll MOD = (1e+9) + 7;
const ll INF = 1LL<<60;
typedef pair<ll, ll> P;

template<class T> bool chmin(T &a, const T &b){
    if(a > b) {a = b; return true;}
    else return false;
}
template<class T> bool chmax(T &a, const T &b){
    if(a < b) {a = b; return true;}
    else return false;
}

int main() {
    cin>>N>>K;
    vector<string> s(N);
    vec cost(N);
    rep(i,N) cin>>s[i]>>cost[i];
    mat num(N, vec(3, 0));
    rep(i,N){
        for(char c : s[i]){
            ++num[i][c == 'J' ? 0 : (c == 'O' ? 1 : 2)];
        }
    }
    vector<mat> dp(3, mat(N + 1, vec(K + 81, INF)));
    dp[0][0][0] = 0;
    string JOI = "JOI";
    rep(c, 3){
        rep(i,c){
            reps(j, max(0LL, K - 80), K + 81) {
                ll temp = dp[i][N][j];
                rep(k, N) {
                    ll rest = (c - i == 2 ? K : max(0LL, K - j));
                    ll r2 = (c - i == 2 ? rest : 0LL);
                    for (char si : s[k]) {
                        if(r2 > 0){
                            r2 -= si == JOI[0];
                        }else if (rest > 0) {
                            rest -= si == JOI[c - 1];
                        } else {
                            rest -= si == JOI[c];
                        }
                    }
                    if (rest <= 0) chmin(dp[c][0][min(K + 80, -rest)], temp + cost[k]);
                }
            }
        }
        /*rep(j,K + 1){
            cout<<dp[c][0][j]<<' ';
        }
        cout<<endl;*/
        rep(i,N){
            rep(j, K + 81){
                chmin(dp[c][i+1][j], dp[c][i][j]);
                if(j >= num[i][c]) chmin(dp[c][i+1][j], dp[c][i+1][j - num[i][c]] + cost[i]);
                //if(j <= K) cout<<dp[c][i+1][j]<<' ';
            }
            //cout<<endl;
        }
    }
    ll ans = INF;
    reps(j,K,K + 81) chmin(ans, dp[2][N][j]);
    cout<<(ans == INF ? -1 : ans)<<endl;
}
0