結果

問題 No.50 おもちゃ箱
ユーザー ei1333
提出日時 2014-11-07 16:40:36
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 11 ms / 5,000 ms
コード長 1,590 bytes
コンパイル時間 1,417 ms
コンパイル使用メモリ 162,312 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-25 20:45:24
合計ジャッジ時間 2,689 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
 
template<typename T1, typename T2> istream& operator>>(istream& is, pair<T1,T2>& a){ return is >> a.first >> a.second; }
template<typename T1, typename T2> ostream& operator<<(ostream& os, pair<T1,T2>& a){ return os << a.first << " "<<a.second; }
template<typename T> istream& operator>>(istream& is, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) is >> vc[i]; return is; }
template<typename T> ostream& operator<<(ostream& os, vector< T >& vc){ for(int i = 0; i < vc.size(); i++) os << vc[i] << endl; return os; }
 
#define ForEach(it,c) for(__typeof (c).begin() it = (c).begin(); it != (c).end(); it++)
#define ALL(v) (v).begin(), (v).end()
#define UNQ(s) { sort(ALL(s)); (s).erase( unique( ALL(s)), (s).end());}

const int INF = 1 << 30;


int main(){

  int N, M;
  vector< int > A, B;
  cin >> N;
  A.resize(N);
  cin >> A;

  cin >> M;
  B.resize(M);
  cin >> B;

  vector< int > Cost(1 << N, 0);
  for(int i = 0; i < 1 << N; i++){
    for(int k = 0; k < N; k++){
      if((i >> k) & 1){
        Cost[i] += A[k];
      }
    }
  }

  vector< int > dp(1 << N, INF);
  dp[0]= 0;
  for(int i = 0; i < M; i++){
    for(int k = (1 << N) - 1; k >= 0; k--){ // 買ってあるやつが1
      if(dp[k] == INF) continue;
      for(int l = (1 << N) - 1; l >= 0; l--){ //買うやつが1
        if(k & l || Cost[l] > B[i]) continue;
        dp[k | l] = min( dp[k | l], dp[k] + 1);
      }
    }
  }
  if(dp[(1 << N) - 1] == INF) cout << -1 << endl;
  else cout << dp[(1 << N) - 1] << endl;
}
0