結果

問題 No.2364 Knapsack Problem
ユーザー yansi819yansi819
提出日時 2024-04-07 15:58:23
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 208 ms / 3,000 ms
コード長 1,353 bytes
コンパイル時間 4,941 ms
コンパイル使用メモリ 263,296 KB
実行使用メモリ 67,768 KB
最終ジャッジ日時 2024-04-07 15:58:32
合計ジャッジ時間 7,933 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,676 KB
testcase_01 AC 2 ms
6,676 KB
testcase_02 AC 4 ms
6,676 KB
testcase_03 AC 3 ms
7,764 KB
testcase_04 AC 5 ms
11,972 KB
testcase_05 AC 7 ms
6,676 KB
testcase_06 AC 4 ms
11,972 KB
testcase_07 AC 45 ms
67,520 KB
testcase_08 AC 2 ms
6,676 KB
testcase_09 AC 23 ms
20,416 KB
testcase_10 AC 17 ms
36,796 KB
testcase_11 AC 18 ms
67,176 KB
testcase_12 AC 202 ms
67,768 KB
testcase_13 AC 199 ms
67,768 KB
testcase_14 AC 200 ms
67,768 KB
testcase_15 AC 206 ms
67,768 KB
testcase_16 AC 208 ms
67,768 KB
testcase_17 AC 201 ms
67,768 KB
testcase_18 AC 201 ms
67,768 KB
testcase_19 AC 207 ms
67,768 KB
testcase_20 AC 206 ms
67,768 KB
testcase_21 AC 198 ms
67,768 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#include <atcoder/all>
using namespace std;
using namespace atcoder;
using ll = long long;
using ld = long double;
using mint = modint998244353;

int N, M, W, A[10], B[10], C[10], D[10];
ll dp[1 << 7][1 << 7][501];

int main() {
  cin >> N >> M >> W;
  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];
  for (int i = 0; i < (1 << N); i++) {
    for (int j = 0; j < (1 << M); j++) {
      for (int k = 0; k <= W; k++) {
        dp[i][j][k] = -1e18;
      }
    }
  }
  dp[0][0][0] = 0;
  ll ans = -1e18;
  for (int i = 0; i < (1 << N); i++) {
    for (int j = 0; j < (1 << M); j++) {
      for (int k = 0; k <= W; k++) {
        ans = max(ans, dp[i][j][k]);
        for (int l = 0; l < N; l++) {
          if (i >> l & 1) continue;
          int ni = i | (1 << l);
          int nxt = k + A[l];
          if (nxt > W) continue;
          dp[ni][j][nxt] = max(dp[ni][j][nxt], dp[i][j][k] + B[l]);
        }
        for (int l = 0; l < M; l++) {
          if (j >> l & 1) continue;
          int nj = j | (1 << l);
          int nxt = k - C[l];
          if (nxt < 0) continue;
          dp[i][nj][nxt] = max(dp[i][nj][nxt], dp[i][j][k] - D[l]);
        }
      }
    }
  }
  cout << ans << endl;
  return 0;
}
0