結果

問題 No.2364 Knapsack Problem
ユーザー twooimptwooimp
提出日時 2023-07-01 02:07:12
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,090 bytes
コンパイル時間 1,494 ms
コンパイル使用メモリ 172,064 KB
実行使用メモリ 68,148 KB
最終ジャッジ日時 2023-09-21 19:48:28
合計ジャッジ時間 10,485 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
using ll=long long;

int main(){
  ll n,m,w;
  cin>>n>>m>>w;
  vector<ll> a(n),b(n),c(m),d(m);
  for(ll i=0;i<n;i++){
    cin>>a[i];
  }
  for(ll i=0;i<n;i++){
    cin>>b[i];
  }
  for(ll i=0;i<m;i++){
    cin>>c[i];
  }
  for(ll i=0;i<m;i++){
    cin>>d[i];
  }
  vector<vector<ll>> dp(1<<(n+m),vector<ll>(w+1,-1e18));
  dp[0][0]=0;
  for(ll s=0;s<(1<<(n+m));s++){
    for(ll i=0;i<=w;i++){
      for(ll j=0;j<n;j++){
        if(s&(1<<j)){
          continue;
        }
        dp[s|(1<<j)][i]=max(dp[s|(1<<j)][i],dp[s][i]);
        if(i+a[j]<=w){
          dp[s|(1<<j)][i+a[j]]=max(dp[s|(1<<j)][i+a[j]],dp[s][i]+b[j]);
        }
      }
      for(ll j=0;j<m;j++){
        if(s&(1<<(j+n))){
          continue;
        }
        dp[s|(1<<(j+n))][i]=max(dp[s|(1<<(j+n))][i],dp[s][i]);
        if(i-a[j]>=0){
          dp[s|(1<<(j+n))][i-c[j]]=max(dp[s|(1<<(j+n))][i-c[j]],dp[s][i]-d[j]);
        }
      }
    }
  }
  ll ans=0;
  for(ll s=0;s<(1<<(n+m));s++){
    for(ll i=0;i<=w;i++){
      ans=max(dp[s][i],ans);
    }
  }
  cout<<ans<<endl;
}
0