結果

問題 No.2364 Knapsack Problem
コンテスト
ユーザー siman
提出日時 2023-07-01 19:02:49
言語 C++17(clang)
(clang++ 22.1.2 + boost 1.89.0)
コンパイル:
clang++ -O2 -lm -std=c++1z -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,414 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 5,220 ms
コンパイル使用メモリ 145,920 KB
実行使用メモリ 7,720 KB
最終ジャッジ日時 2026-03-29 22:05:23
合計ジャッジ時間 4,998 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10 WA * 10
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp:19:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   19 |   int A[N];
      |         ^
main.cpp:19:9: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:17:7: note: declared here
   17 |   int N, M, W;
      |       ^
main.cpp:20:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   20 |   int B[N];
      |         ^
main.cpp:20:9: note: read of non-const variable 'N' is not allowed in a constant expression
main.cpp:17:7: note: declared here
   17 |   int N, M, W;
      |       ^
main.cpp:21:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   21 |   int C[M];
      |         ^
main.cpp:21:9: note: read of non-const variable 'M' is not allowed in a constant expression
main.cpp:17:10: note: declared here
   17 |   int N, M, W;
      |          ^
main.cpp:22:9: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   22 |   int D[M];
      |         ^
main.cpp:22:9: note: read of non-const variable 'M' is not allowed in a constant expression
main.cpp:17:10: note: declared here
   17 |   int N, M, W;
      |          ^
main.cpp:36:17: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   36 |   int dp[W + 1][1 << M];
      |                 ^~~~~~
main.cpp:36:22: note: read of non-const variable 'M' is not allowed in a constant expression
   36 |   int dp[W + 1][1 << M];
      |                      ^
main.cpp:17:10: note: declared here
   17 |   int N, M, W;
      |          ^
main.cpp:36:10: warning: variable length arrays in C++ are a Clang extension [-Wvla-cxx-extension]
   36 |   int dp[W + 1][1 << M];
      |          ^~~~~
main.cpp:36:10: note: read of non-const variable 'W' is not allowed in a constant expression
main.cpp:17:13: note: declared here
   17 |   int N, M, W;
      |             ^
6 warnings generated.

ソースコード

diff #
raw source code

#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