結果

問題 No.2364 Knapsack Problem
ユーザー kotatsugamekotatsugame
提出日時 2023-06-30 21:34:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 3 ms / 3,000 ms
コード長 669 bytes
コンパイル時間 529 ms
コンパイル使用メモリ 66,384 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-21 15:23:12
合計ジャッジ時間 1,578 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 3 ms
4,376 KB
testcase_14 AC 2 ms
4,380 KB
testcase_15 AC 3 ms
4,376 KB
testcase_16 AC 3 ms
4,376 KB
testcase_17 AC 2 ms
4,376 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 3 ms
4,380 KB
testcase_20 AC 3 ms
4,380 KB
testcase_21 AC 3 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<cassert>
using namespace std;
int N,M,W;
int A[7],B[7],C[7],D[7];
bool dp[1<<14];
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cin>>N>>M>>W;
	for(int i=0;i<N;i++)cin>>A[i];
	for(int i=0;i<N;i++)cin>>B[i];
	for(int i=0;i<M;i++)cin>>C[i];
	for(int i=0;i<M;i++)cin>>D[i];
	dp[0]=true;
	int ans=0;
	for(int i=0;i<1<<N+M;i++)if(dp[i])
	{
		int w=0,v=0;
		for(int j=0;j<N+M;j++)if(i>>j&1)
		{
			if(j<N)w+=A[j],v+=B[j];
			else w-=C[j-N],v-=D[j-N];
		}
		ans=max(ans,v);
		for(int j=0;j<N+M;j++)if(!(i>>j&1))
		{
			int nw=w;
			if(j<N)nw+=A[j];
			else nw-=C[j-N];
			if(0<=nw&&nw<=W)dp[i|1<<j]=true;
		}
	}
	cout<<ans<<endl;
}
0