結果

問題 No.1029 JJOOII 3
ユーザー freecss00freecss00
提出日時 2020-04-18 17:05:36
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,582 bytes
コンパイル時間 1,877 ms
コンパイル使用メモリ 176,920 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-14 21:01:32
合計ジャッジ時間 4,861 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define REP(i,m,n) for(int i=(m); i<(int)(n); i++)
#define RREP(i,m,n) for(int i=(int)(n-1); i>=m; i--)
#define rep(i,n) REP(i,0,n)
#define rrep(i,n) RREP(i,0,n)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define fi first
#define se second
#define debug(...) {cerr<<"[L"<<__LINE__<<"] "; _debug(__VA_ARGS__);}

template<typename T>
string join(const vector<T>&v, string del=", "){ stringstream s;
    for(auto x : v) s << del << x; return s.str().substr(del.size());
}
template<typename T>
ostream& operator<<(ostream& o, const vector<T>&v){
    if(v.size()) o << "[" << join(v) << "]"; return o;
}
template<typename T>
ostream& operator<<(ostream& o, const vector<vector<T> >&vv){
    int l = vv.size();
    if(l){ o<<endl; rep(i,l) o << (i==0 ? "[ " : ",\n  " ) << vv[i] << (i==l-1 ? " ]" : ""); }
    return o;
}
inline void _debug(){cerr<<endl;}
template<class First, class... Rest>
void _debug(const First& first, const Rest&... rest){cerr<<first<<" ";_debug(rest...);}

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<vl> vvl;

const double PI = (1*acos(0.0));
const double EPS = 1e-9;
const int INF = 0x3f3f3f3f;
const ll INFL = 0x3f3f3f3f3f3f3f3fLL;
const ll mod = 1e9 + 7;

inline void finput(string filename) {
    freopen(filename.c_str(), "r", stdin);
}

int main(){
    ios_base::sync_with_stdio(0);
    // finput("./input");
    ll n, k; cin >> n >> k;
    vector<string> ss(n); vl cost(n);
    rep(i,n) cin >> ss[i] >> cost[i];

    vector<vvl> ccnt(3, vvl(n));
    rep(i,n){
        rep(c,3) ccnt[c][i] = vl(ss[i].size());
        rep(ii,ss[i].size()){
            int ci = (ss[i][ii] == 'J') ? 0 :
                     (ss[i][ii] == 'O') ? 1 : 2;
            ccnt[ci][i][ii]++;
            if(ii != 0){
                rep(c,3) ccnt[c][i][ii] += ccnt[c][i][ii-1];
            }
        }
    }

    vvl dp(3, vl(k+1, INFL));
    rep(c,3) dp[c][0] = 0;
    rep(c,3) rep(i,n) rep(j,k){
        int l = min(k, j + ccnt[c][i][ss[i].size()-1]);
        dp[c][l] = min(dp[c][l], dp[c][j] + cost[i]);
    }
    rep(c,3){
        if(dp[c][k] == INFL){
            cout << -1 << endl;
            return 0;
        }
    }

    ll ans = INFL;
    rep(i,n){
        int mi = ss[i].size() - 1;
        rep(ii,mi+1){
            if(ccnt[1][i][mi] - ccnt[1][i][ii] >= k){
                REP(jj,ii+1,mi){
                    if(ccnt[1][i][jj] - ccnt[1][i][ii] >= k){
                        ll jcnt = ccnt[0][i][ii];
                        ll icnt = ccnt[2][i][mi] - ccnt[2][i][jj];
                        ll jcost = dp[0][max(k-jcnt, 0ll)];
                        ll icost = dp[2][max(k-icnt, 0ll)];
                        ans = min(ans, cost[i] + jcost + icost);
                    }
                }
            }else{
                rep(j,n){
                    int mj = ss[j].size() - 1;
                    rep(jj,mj+1){
                        ll jcnt = ccnt[0][i][ii];
                        ll ocnt = ccnt[1][i][mi] - ccnt[1][i][ii] + ccnt[1][j][jj];
                        ll icnt = ccnt[2][j][mj] - ccnt[2][j][jj];
                        ll jcost = dp[0][max(k-jcnt, 0ll)];
                        ll ocost = dp[1][max(k-ocnt, 0ll)];
                        ll icost = dp[2][max(k-icnt, 0ll)];
                        ans = min(ans, cost[i] + cost[j] + jcost + ocost + icost);
                    }
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}
0