結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
bool dp[2][1<<10];
int main(){
	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];
	sort(b.begin(),b.end(),greater<int>());
	dp[0][0]=true;
	for(int i=0;i<m;i++){
		for(int t=1;t<1<<n;t++){
			int sum=0;
			for(int j=0;j<n;j++){
				if((t>>j)&1)	sum+=a[j];
			}
			if(sum>b[i])	continue;
			for(int j=0;j<1<<n;j++)	dp[1][t|j]|=dp[0][j];
		}
		for(int j=1;j<1<<n;j++)	dp[0][j]|=dp[1][j];
		if(dp[0][(1<<n)-1]){
			cout<<i+1<<endl;
			return 0;
		}
	}
	cout<<-1<<endl;
	return 0;
}
0