結果

問題 No.2364 Knapsack Problem
ユーザー simansiman
提出日時 2023-07-01 19:02:49
言語 C++17(clang)
(17.0.6 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,414 bytes
コンパイル時間 6,543 ms
コンパイル使用メモリ 102,408 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-22 13:13:22
合計ジャッジ時間 10,442 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 3 ms
4,380 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 AC 2 ms
4,380 KB
testcase_21 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cassert>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <climits>
#include <map>
#include <queue>
#include <set>
#include <cstring>
#include <vector>

using namespace std;
typedef long long ll;

int main() {
  int N, M, W;
  cin >> N >> M >> W;
  int A[N];
  int B[N];
  int C[M];
  int 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 dp[W + 1][1 << M];
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  dp[0][0] = 0;

  for (int i = 0; i < N; ++i) {
    int a = A[i];
    int b = B[i];

    for (int w = W - a; w >= 0; --w) {
      int nw = w + a;

      for (int mask = 0; mask < (1 << M); ++mask) {
        if (dp[w][mask] < 0) continue;
        dp[nw][mask] = max(dp[nw][mask], dp[w][mask] + b);
        ans = max(ans, dp[nw][mask]);
      }
    }

    for (int w = W; w >= 0; --w) {
      for (int mask = 0; mask < (1 << M); ++mask) {
        if (dp[w][mask] < 0) continue;

        for (int j = 0; j < M; ++j) {
          if (mask >> j & 1) continue;
          int nw = w - C[j];
          int nmask = mask | (1 << j);
          if (nw < 0) continue;

          dp[nw][nmask] = max(dp[nw][nmask], dp[w][mask] - D[j]);
        }
      }
    }
  }

  cout << ans << endl;

  return 0;
}
0