結果

問題 No.50 おもちゃ箱
ユーザー imgry22imgry22
提出日時 2015-01-26 01:33:19
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 33 ms / 5,000 ms
コード長 1,438 bytes
コンパイル時間 1,282 ms
コンパイル使用メモリ 147,788 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-08 03:36:16
合計ジャッジ時間 3,279 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 31 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 33 ms
4,380 KB
testcase_06 AC 7 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 1 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 2 ms
4,376 KB
testcase_17 AC 20 ms
4,376 KB
testcase_18 AC 7 ms
4,380 KB
testcase_19 AC 26 ms
4,376 KB
testcase_20 AC 2 ms
4,376 KB
testcase_21 AC 6 ms
4,376 KB
testcase_22 AC 20 ms
4,380 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 1 ms
4,380 KB
testcase_26 AC 2 ms
4,376 KB
testcase_27 AC 8 ms
4,376 KB
testcase_28 AC 32 ms
4,380 KB
testcase_29 AC 32 ms
4,376 KB
testcase_30 AC 32 ms
4,376 KB
testcase_31 AC 2 ms
4,376 KB
testcase_32 AC 32 ms
4,376 KB
testcase_33 AC 32 ms
4,380 KB
testcase_34 AC 32 ms
4,376 KB
testcase_35 AC 32 ms
4,380 KB
testcase_36 AC 32 ms
4,376 KB
testcase_37 AC 32 ms
4,376 KB
testcase_38 AC 32 ms
4,376 KB
testcase_39 AC 32 ms
4,376 KB
testcase_40 AC 32 ms
4,380 KB
testcase_41 AC 32 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int> > vii;
#define rrep(i, m, n) for(int (i)=(m); (i)<(n);  (i)++)
#define erep(i, m, n) for(int (i)=(m); (i)<=(n); (i)++)
#define  rep(i, n)    for(int (i)=0; (i)<(n);  (i)++)
#define  rev(i, n)    for(int (i)=(n)-1; (i)>=0; (i)--)
#define vrep(i, c)    for(__typeof((c).begin())i=(c).begin(); i!=(c).end(); i++)
#define  ALL(v)       (v).begin(), (v).end()
#define mp            make_pair
#define pb            push_back
template<class T1, class T2> inline void minup(T1& m, T2 x){ if(m>x) m=static_cast<T2>(x); }
template<class T1, class T2> inline void maxup(T1& m, T2 x){ if(m<x) m=static_cast<T2>(x); }

#define INF 1000000000
#define MOD 1000000007
#define EPS 1E-12

const int MAX_N = 10;
const int MAX_M = 10;
int n, m;
int a[MAX_N];
int b[MAX_M];
int dp[MAX_M+1][1<<(MAX_N+1)];

int main()
{
  cin >> n;  rep(i, n) cin >> a[i];
  cin >> m;  rep(i, m) cin >> b[i];

  sort(b, b + m, greater<int>());

  rep(i, m+1) rep(j, 1<<n) dp[i][j] = INF;

  dp[0][0] = 0;
  rep(v, m) rep(S, 1<<n){
    minup(dp[v + 1][S], dp[v][S]);

    rep(T, 1<<n) if(!(S & T)){
      int w = 0;
      rep(i, n) if(T & 1 << i) w += a[i];
      if(w > b[v]) continue;
      minup(dp[v + 1][S | T], dp[v][S] + 1);
    }
  }

  cout << (dp[m][(1<<n)-1] >= INF ? -1 : dp[m][(1<<n)-1]) << endl;

  return 0;
}
0