結果

問題 No.2364 Knapsack Problem
ユーザー FplusFplusF
提出日時 2023-06-30 21:55:34
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 2,010 bytes
コンパイル時間 2,060 ms
コンパイル使用メモリ 204,100 KB
最終ジャッジ日時 2025-02-15 03:49:49
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 11 WA * 9
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll=long long;
using pll=pair<ll,ll>;
using tll=tuple<ll,ll,ll>;
using ld=long double;
const ll INF=(1ll<<60);
#define rep(i,n) for (ll i=0;i<(ll)(n);i++)
#define all(v) v.begin(),v.end()
template<class T> void chmin(T &a,T b){
    if(a>b){
        a=b;
    }
}
template<class T> void chmax(T &a,T b){
    if(a<b){
        a=b;
    }
}
int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    ll n,m,w;
    cin >> n >> m >> w;
    vector<ll> a(n),b(n),c(m),d(m);
    rep(i,n) cin >> a[i];
    rep(i,n) cin >> b[i];
    rep(i,m) cin >> c[i];
    rep(i,m) cin >> d[i];
    vector<vector<vector<ll>>> dp(1<<n,vector<vector<ll>>(1<<m,vector<ll>(w+1,-INF)));
    dp[0][0][0]=0;
    rep(i,1<<n){
        rep(j,1<<m){
            rep(k,w+1){
                rep(l,n){
                    if(i&(1<<l)) continue;
                    if(k+a[l]<=w) chmax(dp[i|(1<<l)][j][k+a[l]],dp[i][j][k]+b[l]);
                    chmax(dp[i|(1<<l)][j][k],dp[i][j][k]);
                }
                rep(l,m){
                    if(i&(1<<l)) continue;
                    if(0<=k-c[l]) chmax(dp[i][j|(1<<l)][k-c[l]],dp[i][j][k]-d[l]);
                    chmax(dp[i][j|(1<<l)][k],dp[i][j][k]);
                }
            }
        }
    }
    rep(i,1<<n){
        rep(j,1<<m){
            rep(k,w+1){
                rep(l,m){
                    if(i&(1<<l)) continue;
                    if(0<=k-c[l]) chmax(dp[i][j|(1<<l)][k-c[l]],dp[i][j][k]-d[l]);
                    chmax(dp[i][j|(1<<l)][k],dp[i][j][k]);
                }
                rep(l,n){
                    if(i&(1<<l)) continue;
                    if(k+a[l]<=w) chmax(dp[i|(1<<l)][j][k+a[l]],dp[i][j][k]+b[l]);
                    chmax(dp[i|(1<<l)][j][k],dp[i][j][k]);
                }
            }
        }
    }
    ll ans=0;
    rep(i,1<<n){
        rep(j,1<<m){
            rep(k,w+1){
                chmax(ans,dp[i][j][k]);
            }
        }
    }
    cout << ans << '\n';
}
0