結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#define REP(i, n)           for(int(i)=0;(i)<(n);++(i))

const int INF = 1<<29;
int N,M;
int A[11],B[11];
int sum[1<<10], dp[11][1<<10];

int main(){
    cin >> N;
    REP(i,N) cin >> A[i];
    cin >> M;
    REP(i,M) cin >> B[i];
    REP(i,N){
        int k = 1<<i;
        REP(j,k) sum[k+j] = sum[j] + A[i];
    }

    sort(B, B+M, greater<int>());
    REP(i,M+1) REP(j,1<<10) dp[i][j] = INF;
    dp[0][0] = 0;
    const int fullbit = (1<<N)-1;

    REP(i,M){
        REP(j,1<<N){
            if(dp[i][j] == INF) continue;
            int k = j ^ fullbit;
            for(int l=k; l>0; l=(l-1)&k){
                if(sum[l] > B[i]) continue;
                dp[i+1][j|l] = min(dp[i+1][j|l], dp[i][j]+1);
            }
            dp[i+1][j] = min(dp[i+1][j], dp[i][j]);
        }
    }
    int result = dp[M][fullbit];
    if(result == INF) result = -1;
    cout << result << endl;
}
0