結果

問題 No.50 おもちゃ箱
ユーザー kotatsugame
提出日時 2019-11-21 03:06:32
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 8 ms / 5,000 ms
コード長 618 bytes
コンパイル時間 689 ms
コンパイル使用メモリ 64,896 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-10-08 08:38:17
合計ジャッジ時間 2,231 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 38
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:7:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
    7 | main()
      | ^~~~

ソースコード

diff #

#include<iostream>
using namespace std;
int N,M;
int A[10];
bool dp[11][1<<10];
int sum[1<<10];
main()
{
	cin>>N;
	for(int i=0;i<N;i++)cin>>A[i];
	for(int i=1;i<1<<N;i++)
	{
		for(int j=0;j<N;j++)if(i>>j&1)sum[i]+=A[j];
	}
	cin>>M;
	dp[0][0]=true;
	for(int i=0;i<M;i++)
	{
		int B;cin>>B;
		for(int k=i;k>=0;k--)
		{
			for(int j=1<<N;j--;)
			{
				if(!dp[k][j])continue;
				int sup=((1<<N)-1)&~j;
				int sub=sup;
				while(true)
				{
					if(sum[sub]<=B)dp[k+1][j|sub]=true;
					if(sub==0)break;
					sub=sub-1&sup;
				}
			}
		}
	}
	int id=1;
	while(id<=N&&!dp[id][(1<<N)-1])id++;
	cout<<(id>N?-1:id)<<endl;
}
0