結果

問題 No.1029 JJOOII 3
ユーザー beetbeet
提出日時 2020-04-17 21:32:28
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 58 ms / 2,000 ms
コード長 1,487 bytes
コンパイル時間 2,165 ms
コンパイル使用メモリ 200,084 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-14 13:06:22
合計ジャッジ時間 4,341 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 3 ms
6,816 KB
testcase_01 AC 3 ms
6,940 KB
testcase_02 AC 3 ms
6,944 KB
testcase_03 AC 6 ms
6,940 KB
testcase_04 AC 11 ms
6,944 KB
testcase_05 AC 46 ms
6,940 KB
testcase_06 AC 31 ms
6,940 KB
testcase_07 AC 42 ms
6,940 KB
testcase_08 AC 45 ms
6,944 KB
testcase_09 AC 38 ms
6,940 KB
testcase_10 AC 41 ms
6,940 KB
testcase_11 AC 47 ms
6,944 KB
testcase_12 AC 28 ms
6,944 KB
testcase_13 AC 49 ms
6,944 KB
testcase_14 AC 51 ms
6,944 KB
testcase_15 AC 35 ms
6,944 KB
testcase_16 AC 40 ms
6,944 KB
testcase_17 AC 46 ms
6,944 KB
testcase_18 AC 48 ms
6,940 KB
testcase_19 AC 3 ms
6,940 KB
testcase_20 AC 3 ms
6,940 KB
testcase_21 AC 4 ms
6,940 KB
testcase_22 AC 3 ms
6,944 KB
testcase_23 AC 4 ms
6,940 KB
testcase_24 AC 4 ms
6,940 KB
testcase_25 AC 5 ms
6,940 KB
testcase_26 AC 3 ms
6,944 KB
testcase_27 AC 4 ms
6,940 KB
testcase_28 AC 3 ms
6,944 KB
testcase_29 AC 4 ms
6,940 KB
testcase_30 AC 4 ms
6,944 KB
testcase_31 AC 3 ms
6,940 KB
testcase_32 AC 51 ms
6,940 KB
testcase_33 AC 56 ms
6,940 KB
testcase_34 AC 58 ms
6,944 KB
testcase_35 AC 57 ms
6,940 KB
testcase_36 AC 55 ms
6,940 KB
testcase_37 AC 4 ms
6,940 KB
testcase_38 AC 3 ms
6,944 KB
testcase_39 AC 4 ms
6,944 KB
testcase_40 AC 3 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
using Int = long long;
const char newl = '\n';

//INSERT ABOVE HERE
const Int MAX = 1e5+100;
const Int INF = 1e18;
Int dp[4][MAX];

Int cnt[4][100]={};

signed main(){
  cin.tie(0);
  ios::sync_with_stdio(0);

  for(Int i=0;i<4;i++)
    for(Int j=0;j<MAX;j++)
      dp[i][j]=INF;

  Int n,k;
  cin>>n>>k;

  vector<string> ss(n);
  vector<Int> cs(n);
  for(Int i=0;i<n;i++) cin>>ss[i]>>cs[i];

  string B="JOI$^&";
  for(Int i=0;i<3;i++)
    for(Int p=0;p<n;p++)
      for(char c:ss[p])
        if(c==B[i]) cnt[i][p]++;

  dp[0][0]=0;
  for(Int i=0;i<3;i++){
    for(Int j=0;j<k;j++){
      for(Int p=0;p<n;p++){
        if(j+cnt[i][p]<k){
          chmin(dp[i][j+cnt[i][p]],dp[i][j]+cs[p]);
        }else{
          Int need=k-j,start=0,next=0,nextnext=0;
          for(char c:ss[p]){
            if(c==B[i]) need--;
            if(need<=0 and c==B[i+1]) start++;
            if(start>=k and c==B[i+2]) next++;
            if(next>=k and c==B[i+3]) nextnext++;
          }
          assert(need<=0);
          if(start<k) chmin(dp[i+1][start],dp[i][j]+cs[p]);
          else if(next<k) chmin(dp[i+2][next],dp[i][j]+cs[p]);
          else chmin(dp[i+3][nextnext],dp[i][j]+cs[p]);
        }
      }
    }
  }

  Int ans=dp[3][0];
  if(ans==INF) ans=-1;
  cout<<ans<<newl;
  return 0;
}
0