結果

問題 No.1029 JJOOII 3
ユーザー chocoruskchocorusk
提出日時 2020-04-17 23:03:25
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 117 ms / 2,000 ms
コード長 2,193 bytes
コンパイル時間 1,453 ms
コンパイル使用メモリ 130,624 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 15:22:07
合計ジャッジ時間 4,278 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,816 KB
testcase_01 AC 2 ms
6,944 KB
testcase_02 AC 2 ms
6,940 KB
testcase_03 AC 6 ms
6,940 KB
testcase_04 AC 10 ms
6,944 KB
testcase_05 AC 53 ms
6,940 KB
testcase_06 AC 46 ms
6,944 KB
testcase_07 AC 48 ms
6,940 KB
testcase_08 AC 53 ms
6,940 KB
testcase_09 AC 46 ms
6,940 KB
testcase_10 AC 49 ms
6,944 KB
testcase_11 AC 57 ms
6,940 KB
testcase_12 AC 39 ms
6,944 KB
testcase_13 AC 54 ms
6,944 KB
testcase_14 AC 57 ms
6,940 KB
testcase_15 AC 43 ms
6,944 KB
testcase_16 AC 46 ms
6,940 KB
testcase_17 AC 49 ms
6,940 KB
testcase_18 AC 50 ms
6,944 KB
testcase_19 AC 2 ms
6,944 KB
testcase_20 AC 16 ms
6,940 KB
testcase_21 AC 15 ms
6,940 KB
testcase_22 AC 17 ms
6,940 KB
testcase_23 AC 14 ms
6,944 KB
testcase_24 AC 16 ms
6,940 KB
testcase_25 AC 17 ms
6,944 KB
testcase_26 AC 24 ms
6,940 KB
testcase_27 AC 30 ms
6,940 KB
testcase_28 AC 26 ms
6,940 KB
testcase_29 AC 23 ms
6,944 KB
testcase_30 AC 29 ms
6,940 KB
testcase_31 AC 26 ms
6,940 KB
testcase_32 AC 117 ms
6,940 KB
testcase_33 AC 117 ms
6,944 KB
testcase_34 AC 117 ms
6,944 KB
testcase_35 AC 117 ms
6,944 KB
testcase_36 AC 117 ms
6,944 KB
testcase_37 AC 2 ms
6,944 KB
testcase_38 AC 2 ms
6,944 KB
testcase_39 AC 2 ms
6,940 KB
testcase_40 AC 2 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
	int n, k;
    cin>>n>>k;
    string s[80];
    ll v[80];
    int c[80][3]={};
    int cc[80][81][3]={};
    for(int i=0; i<n; i++){
        cin>>s[i]>>v[i];
        for(int j=0; j<s[i].size(); j++){
            for(int l=0; l<3; l++) cc[i][j+1][l]=cc[i][j][l];
            if(s[i][j]=='J') c[i][0]++, cc[i][j+1][0]++;
            else if(s[i][j]=='O') c[i][1]++, cc[i][j+1][1]++;
            else c[i][2]++, cc[i][j+1][2]++;
        }
    }
    const ll INF=1e18;
    ll dp[3][100010];
    for(int i=0; i<3; i++){
        for(int l=0; l<=k; l++) dp[i][l]=INF;
        dp[i][0]=0;
        for(int j=0; j<n; j++){
            for(int l=0; l<=k; l++){
                dp[i][min(k, l+c[j][i])]=min(dp[i][min(k, l+c[j][i])], dp[i][l]+v[j]);
            }
        }
        for(int l=k-1; l>=0; l--) dp[i][l]=min(dp[i][l], dp[i][l+1]);
    }
    ll ans=INF;
    for(int i=0; i<n; i++){
        if(c[i][1]<k) continue;
        for(int p=0; p<=s[i].size(); p++){
            for(int q=p+1; q<=s[i].size(); q++){
                if(cc[i][q][1]-cc[i][p][1]<k) continue;
                ans=min(ans, v[i]+dp[0][max(0, k-cc[i][p][0])]+dp[2][max(0, k-(c[i][2]-cc[i][q][2]))]);
            }
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            for(int p=0; p<=s[i].size(); p++){
                for(int q=0; q<=s[j].size(); q++){
                    ans=min(ans, v[i]+v[j]+dp[0][max(0, k-cc[i][p][0])]+dp[1][max(0, k-(c[i][1]-cc[i][p][1])-cc[j][q][1])]+dp[2][max(0, k-(c[j][2]-cc[j][q][2]))]);
                }
            }
        }
    }
    if(ans>=INF/2) cout<<-1<<endl;
    else cout<<ans<<endl;
	return 0;
}
0