結果

問題 No.50 おもちゃ箱
コンテスト
ユーザー pessimist
提出日時 2025-10-17 20:26:01
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 3 ms / 5,000 ms
コード長 1,053 bytes
コンパイル時間 3,192 ms
コンパイル使用メモリ 289,548 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2025-10-17 20:26:06
合計ジャッジ時間 5,080 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

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

void solve(){
    int N;cin>>N;
    vector<int>A(N);
    for(auto&x:A) cin>>x;
    int M;cin>>M;
    vector<int> B(M);
    for(auto&x:B)cin>>x;
    sort(B.rbegin(),B.rend());

    int full=1<<N;
    vector<int> toy_sub(full);
    for(int mask=1;mask<full;++mask){
        int last_bit=mask&-mask;
        int i=__builtin_ctz(last_bit);
        toy_sub[mask]=toy_sub[mask^last_bit]+A[i];
    }

    vector<bool> dp(1<<N);
    dp[0]=true;
    int ans=-1;
    for(int n=1;n<=M;++n){
        vector<bool> ndp(1<<N);
        for(int s=0;s<full;++s){
            int j=s;
            while(1){
                if(toy_sub[j]<=B[n-1] && dp[s^j]){
                    ndp[s]=1;
                    break;
                }
                if(!j)break;
                j=(j-1)&s;
            }
        }
        dp=ndp;
        if(dp.back()){
            ans=n;
            break;
        }
    }
    cout<<ans<<'\n';
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    solve();
    return 0;
}
0