結果

問題 No.2364 Knapsack Problem
ユーザー FplusFplusFFplusFplusF
提出日時 2023-06-30 22:02:40
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 391 ms / 3,000 ms
コード長 1,299 bytes
コンパイル時間 2,115 ms
コンパイル使用メモリ 204,304 KB
実行使用メモリ 67,876 KB
最終ジャッジ日時 2023-09-21 16:02:47
合計ジャッジ時間 7,055 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 3 ms
4,380 KB
testcase_05 AC 9 ms
5,044 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 37 ms
8,648 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 36 ms
10,164 KB
testcase_10 AC 14 ms
4,940 KB
testcase_11 AC 7 ms
4,436 KB
testcase_12 AC 385 ms
67,672 KB
testcase_13 AC 375 ms
67,656 KB
testcase_14 AC 381 ms
67,628 KB
testcase_15 AC 391 ms
67,740 KB
testcase_16 AC 379 ms
67,616 KB
testcase_17 AC 374 ms
67,736 KB
testcase_18 AC 390 ms
67,612 KB
testcase_19 AC 389 ms
67,720 KB
testcase_20 AC 382 ms
67,748 KB
testcase_21 AC 378 ms
67,876 KB
権限があれば一括ダウンロードができます

ソースコード

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