結果

問題 No.50 おもちゃ箱
ユーザー hotpepsi
提出日時 2015-07-03 23:49:05
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 73 ms / 5,000 ms
コード長 627 bytes
コンパイル時間 591 ms
コンパイル使用メモリ 61,044 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-06-25 20:57:15
合計ジャッジ時間 1,982 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#include <sstream>

using namespace std;

int N, M;
int A[10];
int B[10];

int main(int argc, char *argv[])
{
	cin >> N;
	for (int i = 0; i < N; ++i) {
		cin >> A[i];
	}
	cin >> M;
	for (int i = 0; i < M; ++i) {
		cin >> B[i];
	}

	sort(A, A + N);
	sort(B, B + M);

	int ans = 1 << 30;
	do {
		int i = 0;
		for (int j = 0; j < M; ++j) {
			int r = B[M - j - 1];
			while (i < N && r >= A[i]) {
				r -= A[i];
				++i;
			}
			if (i >= N) {
				ans = min(ans, j + 1);
				break;
			}
		}
	} while (next_permutation(A, A + N));
	cout << (ans < (1 << 30) ? ans : -1) << endl;
	return 0;
}
0