結果

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
  #include "settings/debug.cpp"
  #define _GLIBCXX_DEBUG
#else
  #define Debug(...) void(0)
#endif
using ll = long long;
#define rep(i, n) for (int i = 0; i < (n); ++i)

int main() {
  int n, m, w;
  cin >> n >> m >> w;
  vector<int> 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 dp(1 << (n + m), vector<ll>(w + 1, -1));
  dp[0][0] = 0;
  rep(bit, 1 << (n + m)) {
    rep(j, w + 1) {
      if (dp[bit][j] == -1) continue;
      rep(i, n) {
        if (bit & (1 << i)) continue;
        if (j + a[i] > w) continue;
        dp[bit | (1 << i)][j + a[i]] = max(dp[bit | (1 << i)][j + a[i]], dp[bit][j] + b[i]);
      }
      rep(i, m) {
        if (bit & (1 << (n + i))) continue;
        if (j - c[i] < 0) continue;
        dp[bit | (1 << (i + n))][j - c[i]] = max(dp[bit | (1 << (i + n))][j - c[i]], dp[bit][j] - d[i]);
      }
    }
  }
  ll ans = 0;
  rep(bit, 1 << (n + m)) rep(j, w + 1) ans = max(ans, dp[bit][j]);
  cout << ans << endl;
  return 0;
}
0