結果

問題 No.1029 JJOOII 3
ユーザー mugen_1337mugen_1337
提出日時 2020-07-09 21:00:34
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 3,034 bytes
コンパイル時間 2,389 ms
コンパイル使用メモリ 200,136 KB
実行使用メモリ 5,760 KB
最終ジャッジ日時 2024-04-16 23:57:47
合計ジャッジ時間 5,181 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 3 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 6 ms
5,376 KB
testcase_04 AC 10 ms
5,376 KB
testcase_05 AC 53 ms
5,376 KB
testcase_06 AC 48 ms
5,376 KB
testcase_07 AC 48 ms
5,376 KB
testcase_08 AC 54 ms
5,376 KB
testcase_09 AC 48 ms
5,376 KB
testcase_10 WA -
testcase_11 AC 58 ms
5,632 KB
testcase_12 AC 41 ms
5,376 KB
testcase_13 AC 53 ms
5,632 KB
testcase_14 AC 55 ms
5,632 KB
testcase_15 AC 21 ms
5,376 KB
testcase_16 AC 24 ms
5,376 KB
testcase_17 AC 28 ms
5,376 KB
testcase_18 AC 50 ms
5,376 KB
testcase_19 WA -
testcase_20 AC 19 ms
5,376 KB
testcase_21 AC 18 ms
5,376 KB
testcase_22 AC 21 ms
5,376 KB
testcase_23 AC 17 ms
5,376 KB
testcase_24 AC 20 ms
5,376 KB
testcase_25 AC 22 ms
5,376 KB
testcase_26 AC 30 ms
5,376 KB
testcase_27 AC 38 ms
5,376 KB
testcase_28 AC 32 ms
5,376 KB
testcase_29 AC 28 ms
5,376 KB
testcase_30 AC 36 ms
5,376 KB
testcase_31 AC 33 ms
5,376 KB
testcase_32 AC 133 ms
5,632 KB
testcase_33 AC 133 ms
5,632 KB
testcase_34 AC 133 ms
5,632 KB
testcase_35 AC 132 ms
5,632 KB
testcase_36 AC 132 ms
5,760 KB
testcase_37 AC 1 ms
5,376 KB
testcase_38 WA -
testcase_39 AC 2 ms
5,376 KB
testcase_40 AC 2 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define ALL(x) x.begin(),x.end()
#define rep(i,n) for(int i=0;i<(n);i++)
#define debug(v) cout<<#v<<":";for(auto x:v){cout<<x<<' ';}cout<<endl;
#define mod 1000000007
using ll=long long;
const int INF=1000000000;
const ll LINF=1001002003004005006ll;
int dx[]={1,0,-1,0};
int dy[]={0,1,0,-1};
// ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
template<class T>bool chmax(T &a,const T &b){if(a<b){a=b;return true;}return false;}
template<class T>bool chmin(T &a,const T &b){if(b<a){a=b;return true;}return false;}

struct IOSetup{
    IOSetup(){
        cin.tie(0);
        ios::sync_with_stdio(0);
        cout<<fixed<<setprecision(12);
    }
} iosetup;

template<typename T1,typename T2>
ostream &operator<<(ostream &os,const pair<T1,T2>&p){
    os<<p.first<<" "<<p.second;
    return os;
}

template<typename T>
ostream &operator<<(ostream &os,const vector<T>&v){
    for(int i=0;i<(int)v.size();i++) os<<v[i]<<(i+1==v.size()?"":" ");
    return os;
}

int n,k;
ll cost[100];
string s[100];


vector<ll> knapsack(char target){
    int cnt[n]={};
    rep(i,n)rep(j,s[i].size())cnt[i]+=(int)(s[i][j]==target);

    vector<ll> dp(k+1,LINF);
    dp[0]=0;
    rep(i,n){
        for(int j=0;j<=k;j++){
            if(j-cnt[i]>=0){
                chmin(dp[j],dp[j-cnt[i]]+cost[i]);
            }
        }
    }
    ll m=LINF;
    for(int i=k;i>=0;i--){
        chmin(m,dp[i]);
        dp[i]=m;
    }
    return dp;
}

signed main(){
    cin>>n>>k;
    rep(i,n) cin>>s[i]>>cost[i];

    auto dpj=knapsack('J');
    auto dpo=knapsack('O');
    auto dpi=knapsack('I');

    if(dpj[k]==LINF or dpo[k]==LINF or dpi[k]==LINF){
        cout<<-1<<endl;
        return 0;
    }


    int cj[n][100]={},co[n][100]={},ci[n][100]={};
    rep(i,n){
        rep(j,s[i].size()){
            cj[i][j+1]=cj[i][j];
            co[i][j+1]=co[i][j];
            ci[i][j+1]=ci[i][j];
            if(s[i][j]=='J') cj[i][j+1]++;
            if(s[i][j]=='O') co[i][j+1]++;
            if(s[i][j]=='I') ci[i][j+1]++;
        }
    }

    ll ans=dpj[k]+dpo[k]+dpi[k];
    // JO, OI
    rep(i,n){
        rep(j,n){
            rep(di,s[i].size()+1){
                rep(dj,s[j].size()+1){
                    int jaru=cj[i][di],oaru=co[i][s[i].size()]-co[i][di]+co[j][dj],iaru=ci[j][s[j].size()]-ci[j][dj];
                    int needj=max(0,k-jaru),needo=max(0,k-oaru),needi=max(0,k-iaru);
                    chmin(ans,cost[i]+cost[j]+dpi[needi]+dpj[needj]+dpo[needo]);
                }
            }
        }
    }


    // JJJO...OIII...
    if(k<=80){
        rep(i,n){
            rep(l,s[i].size()){
                for(int r=l+1;r<=s[i].size();r++){
                    if(co[i][r]-co[i][l]>=k){
                        int jaru=cj[i][l],iaru=ci[i][s[i].size()]-ci[i][r];
                        int needj=max(0,k-jaru),needi=max(0,k-iaru);
                        chmin(ans,cost[i]+dpj[needj]+dpi[needi]);
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
    return 0;
}
0