結果

問題 No.50 おもちゃ箱
ユーザー momoyuumomoyuu
提出日時 2023-10-24 17:45:33
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 452 ms / 5,000 ms
コード長 1,127 bytes
コンパイル時間 3,251 ms
コンパイル使用メモリ 252,140 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-24 17:45:41
合計ジャッジ時間 8,273 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 4 ms
4,348 KB
testcase_01 AC 286 ms
4,348 KB
testcase_02 AC 2 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,348 KB
testcase_05 AC 336 ms
4,348 KB
testcase_06 AC 8 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 1 ms
4,348 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,348 KB
testcase_13 AC 2 ms
4,348 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,348 KB
testcase_16 AC 2 ms
4,348 KB
testcase_17 AC 8 ms
4,348 KB
testcase_18 AC 3 ms
4,348 KB
testcase_19 AC 53 ms
4,348 KB
testcase_20 AC 2 ms
4,348 KB
testcase_21 AC 4 ms
4,348 KB
testcase_22 AC 21 ms
4,348 KB
testcase_23 AC 1 ms
4,348 KB
testcase_24 AC 1 ms
4,348 KB
testcase_25 AC 1 ms
4,348 KB
testcase_26 AC 2 ms
4,348 KB
testcase_27 AC 4 ms
4,348 KB
testcase_28 AC 115 ms
4,348 KB
testcase_29 AC 95 ms
4,348 KB
testcase_30 AC 58 ms
4,348 KB
testcase_31 AC 2 ms
4,348 KB
testcase_32 AC 289 ms
4,348 KB
testcase_33 AC 280 ms
4,348 KB
testcase_34 AC 452 ms
4,348 KB
testcase_35 AC 162 ms
4,348 KB
testcase_36 AC 225 ms
4,348 KB
testcase_37 AC 180 ms
4,348 KB
testcase_38 AC 75 ms
4,348 KB
testcase_39 AC 294 ms
4,348 KB
testcase_40 AC 422 ms
4,348 KB
testcase_41 AC 205 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false); 

    int n;
    cin>>n;
    vector<int> a(n);
    for(int i = 0;i<n;i++) cin>>a[i];
    int m;
    cin>>m;
    vector<int> b(m);
    for(int i = 0;i<m;i++) cin>>b[i];
    vector<vector<char>> dp(1<<n,vector<char>(1<<m,0));
    dp[0][0] = 1;
    vector<int> sum(1<<n,0);
    for(int i = 0;i<1<<n;i++){
        for(int j = 0;j<n;j++) if(i>>j&1) sum[i] += a[j];
    }
    int mask = (1<<n) - 1;
    for(int i = 0;i<1<<n;i++){
        for(int j = 0;j<1<<m;j++){
            if(dp[i][j]==0) continue;
            int rest = mask ^ i;
            for(int k = rest;k>=0;k--){
                k &= rest;
                if(k==0) continue;
                for(int l = 0;l<m;l++){
                    if(j>>l&1) continue;
                    if(b[l]>=sum[k]) dp[i|k][j|(1<<l)] = 1;
                }
            }
        }
    }
    int ans = 1e9;
    for(int i = 0;i<1<<m;i++){
        if(dp[mask][i]) ans = min(ans,__builtin_popcount(i));
    }


    if(ans==1e9) ans = -1;
    cout<<ans<<endl;
}

0