結果

問題 No.1029 JJOOII 3
ユーザー i_mrhji_mrhj
提出日時 2020-05-16 04:01:16
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 4,581 bytes
コンパイル時間 1,071 ms
コンパイル使用メモリ 103,908 KB
実行使用メモリ 10,240 KB
最終ジャッジ日時 2024-09-19 20:19:49
合計ジャッジ時間 2,790 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
6,784 KB
testcase_01 AC 2 ms
6,784 KB
testcase_02 AC 3 ms
6,784 KB
testcase_03 AC 6 ms
7,296 KB
testcase_04 AC 11 ms
9,216 KB
testcase_05 AC 37 ms
9,856 KB
testcase_06 AC 28 ms
8,960 KB
testcase_07 AC 36 ms
9,600 KB
testcase_08 AC 37 ms
9,728 KB
testcase_09 WA -
testcase_10 AC 35 ms
9,472 KB
testcase_11 AC 44 ms
10,012 KB
testcase_12 WA -
testcase_13 AC 41 ms
10,112 KB
testcase_14 AC 43 ms
10,112 KB
testcase_15 AC 4 ms
6,784 KB
testcase_16 AC 4 ms
6,784 KB
testcase_17 AC 4 ms
6,784 KB
testcase_18 AC 28 ms
9,984 KB
testcase_19 AC 4 ms
6,784 KB
testcase_20 AC 4 ms
6,656 KB
testcase_21 AC 3 ms
6,784 KB
testcase_22 AC 4 ms
6,912 KB
testcase_23 WA -
testcase_24 AC 5 ms
6,656 KB
testcase_25 AC 5 ms
6,784 KB
testcase_26 WA -
testcase_27 AC 4 ms
6,784 KB
testcase_28 AC 4 ms
6,784 KB
testcase_29 AC 3 ms
6,784 KB
testcase_30 AC 3 ms
6,656 KB
testcase_31 AC 3 ms
6,784 KB
testcase_32 AC 44 ms
10,240 KB
testcase_33 AC 45 ms
10,016 KB
testcase_34 AC 46 ms
10,240 KB
testcase_35 AC 45 ms
10,112 KB
testcase_36 AC 46 ms
10,020 KB
testcase_37 AC 3 ms
6,656 KB
testcase_38 AC 4 ms
6,784 KB
testcase_39 AC 4 ms
6,784 KB
testcase_40 AC 3 ms
6,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <climits>
#include <cmath>
#include <iostream>
#include <iomanip>
#include <string>
#include <cstdio>
#include <climits>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
#include <utility>
#include <queue>
#include <cstring>
#include <set>
#include <map>
#include <complex>

#define rep(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
long long MOD = 1000000007;
long long INF = 1000000000000000; //10^15
typedef long long ll;
typedef unsigned long long ull;


ll powMod(ll x, ll n, ll mod) {
  if (n == 0) return 1;
  ll t = powMod(x, n/2, mod);
  t = t * t % mod;
  if (n & 1) return t * x % mod;
  return t;
}

ll gcd(ll a, ll b) {
  if (a == 0 || b == 0) return a + b;
  if (b > a) return gcd(b, a);
  return gcd(b, a % b);
}

ll c[100300], mj[100300], mo[100300], mi[100300];
int mjm[100300], mom[100300], mim[100300];

int main(void) {

  int n, k;
  string s[100300];
  
  scanf("%d %d", &n, &k);
  rep(i, n) {
    cin >> s[i];
    scanf("%lld", &c[i]);
  }

  vector< pair<int, int> > J, O, I;
  rep(k, n) {
    int j = 0, o = 0, i = 0;
    rep(l, s[k].size()) {
      if (s[k][l] == 'J') j++;
      if (s[k][l] == 'O') o++;
      if (s[k][l] == 'I') i++;
    }
    if (j > 0) J.push_back(make_pair(k, j));
    if (o > 0) O.push_back(make_pair(k, o));
    if (i > 0) I.push_back(make_pair(k, i));
  }

  //cout << endl;
  //rep(i, I.size()) cout << s[I[i].first] << " " << I[i].second << endl;

  if (J.size() * O.size() * I.size() == 0) {
    printf("-1\n");
    return 0;
  }

  for (int i = 1; i <= k + 100; i++) mj[i] = INF, mo[i] = INF, mi[i] = INF;

  for (int i = 1; i <= k + 100; i++) {
    //cout << -1 << endl;
    rep(j, J.size()) {
      if (J[j].second > i) ;
      else mj[i] = min(mj[i], mj[i-J[j].second]+c[J[j].first]);
    }
    rep(j, O.size()) {
      if (O[j].second > i) ;
      else mo[i] = min(mo[i], mo[i-O[j].second]+c[O[j].first]);
    }
    rep(j, I.size()) {
      if (I[j].second > i) ;
      else mi[i] = min(mi[i], mi[i-I[j].second]+c[I[j].first]);
    }
  }

  mjm[k + 100] = k + 100;
  mom[k + 100] = k + 100;
  mim[k + 100] = k + 100;
  for (int i = k + 99; i >= 0; i--) {
    if (mj[i] < mj[mjm[i+1]]) mjm[i] = i;
    else mjm[i] = mjm[i+1];
    if (mi[i] < mi[mim[i+1]]) mim[i] = i;
    else mim[i] = mim[i+1];
    if (mo[i] < mo[mom[i+1]]) mom[i] = i;
    else mom[i] = mom[i+1];
  }
    

  
  //rep(i, k) cout << mj[i] << endl;
  //cout << endl;
  //cout << "I" << endl;
  //rep(i, k + 5) cout << mi[i] << endl;
  //cout << endl;

  ll f[3][100] = {};
  int r1 = 0;
  rep(i, J.size()) {
    int jc = 0, oc = 0, ic = 0;
    if (J[i].second >= k) {
      rep(j, s[J[i].first].size()) {
	if (s[J[i].first][j] == 'J') jc++;
	if (jc >= k && s[J[i].first][j] == 'O') oc++;
	if (oc >= k && s[J[i].first][j] == 'I') ic++;
      }
      f[0][r1] = c[J[i].first];
      f[1][r1] = oc;
      f[2][r1] = ic;
      r1++;
    } else {
      int e = k - J[i].second;
      e = mjm[e];
      rep(j, s[J[i].first].size()) {
	if (s[J[i].first][j] == 'J') jc++;
	if (jc >= k - e && s[J[i].first][j] == 'O') oc++;
	if (oc >= k && s[J[i].first][j] == 'I') ic++;
      }
      f[0][r1] = mj[e] + c[J[i].first];
      f[1][r1] = oc;
      f[2][r1] = ic;
      r1++;
    }
  }

  ll b[2][100];
  int r2 = 0;
  rep(i, I.size()) {
    int ic = 0, oc = 0;
    if (I[i].second >= k) {
      rep(j, s[I[i].first].size()) {
	if (s[I[i].first][s[I[i].first].size()-1-j] == 'I') ic++;
	if (ic >= k && s[I[i].first][s[I[i].first].size()-1-j] == 'O') oc++;
      }
      b[0][r2] = c[I[i].first];
      b[1][r2] = oc;
      r2++;
    } else {
      int e = k - I[i].second;
      e = mim[e];
      rep(j, s[I[i].first].size()) {
	if (s[I[i].first][s[I[i].first].size()-1-j] == 'I') ic++;
	if (ic >= k - e && s[I[i].first][s[I[i].first].size()-1-j] == 'O') oc++;
      }
      b[0][r2] = mi[e] + c[I[i].first];
      b[1][r2] = oc;
      r2++;
    }
  }

  //rep(i, r1) cout << f[0][i] << " " << f[1][i] << " " << f[2][i] << endl;
  //rep(i, r2) cout << b[0][i] << " " << b[1][i] << endl;

  ll ans = LLONG_MAX;
  rep(i, r1) {
    if (f[2][i] >= k) ans = min(ans, f[0][i]);
    else if (0 < f[2][i] && f[2][i] < k) {
      int e = k - f[2][i];
      e = mim[e];
      ans = min(ans, f[0][i] + mi[e]);
    } else {
      //if (i == 2) cout << 99999 << endl;
      rep(j, r2) {
	int z = k - f[1][i] - b[1][j];
	
	if (z <= 0) ans = min(ans, f[0][i] + b[0][j]);
	else {
	  z = mom[z];
	  ans = min(ans, f[0][i] + b[0][j] + mo[z]);
	}
      }
    }
  }

  cout << ans << endl;

  return 0;

}

  
      
	
0