結果

問題 No.50 おもちゃ箱
ユーザー atn112323atn112323
提出日時 2017-01-03 12:58:47
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 24 ms / 5,000 ms
コード長 853 bytes
コンパイル時間 1,505 ms
コンパイル使用メモリ 52,344 KB
実行使用メモリ 4,508 KB
最終ジャッジ日時 2023-09-08 04:48:17
合計ジャッジ時間 2,471 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 24 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,384 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 23 ms
4,384 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,380 KB
testcase_13 AC 2 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 2 ms
4,384 KB
testcase_16 AC 2 ms
4,380 KB
testcase_17 AC 15 ms
4,376 KB
testcase_18 AC 5 ms
4,380 KB
testcase_19 AC 20 ms
4,376 KB
testcase_20 AC 2 ms
4,380 KB
testcase_21 AC 5 ms
4,376 KB
testcase_22 AC 15 ms
4,380 KB
testcase_23 AC 2 ms
4,508 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 2 ms
4,384 KB
testcase_27 AC 6 ms
4,376 KB
testcase_28 AC 24 ms
4,376 KB
testcase_29 AC 24 ms
4,376 KB
testcase_30 AC 24 ms
4,380 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 24 ms
4,384 KB
testcase_33 AC 24 ms
4,380 KB
testcase_34 AC 24 ms
4,380 KB
testcase_35 AC 24 ms
4,380 KB
testcase_36 AC 24 ms
4,380 KB
testcase_37 AC 24 ms
4,376 KB
testcase_38 AC 24 ms
4,380 KB
testcase_39 AC 24 ms
4,376 KB
testcase_40 AC 24 ms
4,376 KB
testcase_41 AC 24 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>

using namespace std;

const int MAXN = 10;
const int MAXM = 10;

int N, M;
int A[MAXN], B[MAXM];
int dp[MAXM + 1][1 << MAXN];

int main() {
	cin >> N;
	for (int i = 0; i < N; i++) {
		cin >> A[i];
	}
	cin >> M;
	for (int i = 0; i < M; i++) {
		cin >> B[i];
	}
	for (int i = 0; i < 1 << N; i++) {
		dp[0][i] = M + 1;
	}
	dp[0][0] = 0;
	for (int i = 0; i < M; i++) {
		for (int j = 0; j < 1 << N; j++) {
			dp[i + 1][j] = dp[i][j];
			int comb = j;
			while (comb != 0) {
				int sw = 0;
				for (int k = 0; k < N; k++) {
					if (((comb >> k) & 1) == 1) {
						sw += A[k];
					}
				}
				if (B[i] >= sw) {
					dp[i + 1][j] = min(dp[i + 1][j], dp[i][j ^ comb] + 1);
				}
				comb = j & (comb - 1);
			}
		}
	}
	if (dp[M][(1 << N) - 1] <= M) {
		cout << dp[M][(1 << N) - 1] << endl;
	} else {
		cout << -1 << endl;
	}
	return 0;
}
0