結果

問題 No.50 おもちゃ箱
ユーザー veqcc
提出日時 2019-02-16 18:09:01
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 16 ms / 5,000 ms
コード長 1,183 bytes
コンパイル時間 963 ms
コンパイル使用メモリ 94,724 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-22 19:06:48
合計ジャッジ時間 2,389 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <cmath>
#include <stack>
#include <set>
#include <map>
typedef long long ll;
typedef unsigned int uint;
using namespace std;

int n, m, a[10], b[11], sm[1 << 10], dp[11][1 << 10];

int main() {
    cin >> n;
    for (int i = 0; i < n; i++) cin >> a[i];

    for (int i = 0; i < (1 << n); i++) {
        for (int j = 0; j < n; j++) {
            if (i & (1 << j)) {
                sm[i] += a[j];
            }
        }
    }

    cin >> m;
    for (int i = 1; i <= m; i++) cin >> b[i];

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

    dp[0][0] = 1;
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < (1 << n); j++) {
            for (int k = 0; k < (1 << n); k++) {
                if (dp[i][j] == 0) continue;
                if (j & k) continue;
                if (sm[k] > b[i+1]) continue;

                dp[i+1][j | k] = 1;
                if ((j | k) == (1 << n) - 1) {
                    cout << i + 1 << "\n";
                    return 0;
                }
            }
        }
    }

    cout << "-1\n";
    return 0;
}
0