結果

問題 No.2364 Knapsack Problem
ユーザー rulerruler
提出日時 2023-07-01 05:32:00
言語 C++23
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 139 ms / 3,000 ms
コード長 1,157 bytes
コンパイル時間 3,462 ms
コンパイル使用メモリ 251,136 KB
実行使用メモリ 68,008 KB
最終ジャッジ日時 2023-09-21 23:39:37
合計ジャッジ時間 5,263 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 4 ms
5,244 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 12 ms
8,592 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 13 ms
10,528 KB
testcase_10 AC 5 ms
5,116 KB
testcase_11 AC 3 ms
4,696 KB
testcase_12 AC 132 ms
67,656 KB
testcase_13 AC 127 ms
67,660 KB
testcase_14 AC 136 ms
67,704 KB
testcase_15 AC 139 ms
67,672 KB
testcase_16 AC 133 ms
67,972 KB
testcase_17 AC 134 ms
67,724 KB
testcase_18 AC 128 ms
67,720 KB
testcase_19 AC 138 ms
68,008 KB
testcase_20 AC 136 ms
67,700 KB
testcase_21 AC 130 ms
67,668 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
auto main() -> int {
  int n, m, W;
  cin >> n >> m >> W;
  vector<int> a(n), b(n), c(m), d(m);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }

  for (int i = 0; i < n; i++) {
    cin >> b[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> c[i];
  }
  for (int i = 0; i < m; i++) {
    cin >> d[i];
  }
  int l = n + m;
  int l2 = 1 << l;

  const long inf = 1l << 60;
  vector<vector<long>> dp(l2, vector<long>(W + 1, -inf));
  dp[0][0] = 0;
  for (int s = 0; s < l2; s++) {
    for (int i = 0; i < l; i++) {
      if (s >> i & 1)
        continue;
      int u = s | 1 << i;
      for (int j = 0; j <= W; j++) {
        dp[u][j] = max(dp[u][j], dp[s][j]);
      }
      if (i < n) {
        int dw = a[i];
        int dv = b[i];
        for (int j = W; j >= dw; j--) {
          dp[u][j] = max(dp[u][j], dp[s][j - dw] + dv);
        }
      } else {
        int dw = c[i - n];
        int dv = d[i - n];
        for (int j = 0; j <= W - dw; j++) {
          dp[u][j] = max(dp[u][j], dp[s][j + dw] - dv);
        }
      }
    }
  }
  cout << *max_element(dp[l2 - 1].begin(), dp[l2 - 1].end()) << endl;
}
0