結果

問題 No.2364 Knapsack Problem
ユーザー a01sa01toa01sa01to
提出日時 2023-06-30 21:42:13
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 73 ms / 3,000 ms
コード長 1,099 bytes
コンパイル時間 1,964 ms
コンパイル使用メモリ 204,452 KB
実行使用メモリ 67,924 KB
最終ジャッジ日時 2023-09-21 15:35:51
合計ジャッジ時間 3,575 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 3 ms
5,256 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 7 ms
8,628 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 8 ms
10,512 KB
testcase_10 AC 3 ms
5,100 KB
testcase_11 AC 3 ms
4,688 KB
testcase_12 AC 71 ms
67,832 KB
testcase_13 AC 69 ms
67,704 KB
testcase_14 AC 69 ms
67,924 KB
testcase_15 AC 70 ms
67,856 KB
testcase_16 AC 70 ms
67,652 KB
testcase_17 AC 70 ms
67,652 KB
testcase_18 AC 73 ms
67,824 KB
testcase_19 AC 73 ms
67,708 KB
testcase_20 AC 71 ms
67,692 KB
testcase_21 AC 71 ms
67,776 KB
権限があれば一括ダウンロードができます

ソースコード

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