結果

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

ソースコード

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<ll>> dp(1<<(n+m),vector<ll>(w+1,-INF));
    dp[0][0]=0;
    rep(i,1<<(n+m)){
        rep(j,w+1){
            rep(l,n){
                if(i&(1<<l)) continue;
                if(j+a[l]<=w) chmax(dp[i|(1<<l)][j+a[l]],dp[i][j]+b[l]);
                chmax(dp[i|(1<<l)][j],dp[i][j]);
            }
            rep(l,m){
                if(i&(1<<(n+l))) continue;
                if(0<=j-c[l]) chmax(dp[i|(1<<(n+l))][j-c[l]],dp[i][j]-d[l]);
                chmax(dp[i|(1<<(n+l))][j],dp[i][j]);
            }
        }
    }
    ll ans=0;
    rep(i,1<<(n+m)){
        rep(j,w+1){
            chmax(ans,dp[i][j]);
        }
    }
    cout << ans << '\n';
}
0