結果
問題 | No.2364 Knapsack Problem |
ユーザー | kotatsugame |
提出日時 | 2023-06-30 21:34:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 3 ms / 3,000 ms |
コード長 | 669 bytes |
コンパイル時間 | 580 ms |
コンパイル使用メモリ | 66,708 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-07-07 09:07:45 |
合計ジャッジ時間 | 1,268 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 2 |
other | AC * 20 |
ソースコード
#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; }