結果

問題 No.2741 Balanced Choice
ユーザー Nzt3Nzt3
提出日時 2024-04-13 13:24:17
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 37 ms / 2,000 ms
コード長 948 bytes
コンパイル時間 2,351 ms
コンパイル使用メモリ 199,908 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-13 13:25:49
合計ジャッジ時間 3,399 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
6,816 KB
testcase_01 AC 33 ms
6,944 KB
testcase_02 AC 35 ms
6,940 KB
testcase_03 AC 36 ms
6,944 KB
testcase_04 AC 37 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 15 ms
6,940 KB
testcase_08 AC 23 ms
6,940 KB
testcase_09 AC 14 ms
6,940 KB
testcase_10 AC 17 ms
6,940 KB
testcase_11 AC 24 ms
6,944 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
void chmax(int &a, int b) { a = max(a, b); }
int main() {
  int N, W, D;
  cin >> N >> W >> D;
  vector A0(0, array<int, 2>()), A1(0, array<int, 2>());
  for (int i = 0; i < N; i++) {
    int T, W, V;
    cin >> T >> W >> V;
    if (T == 0) {
      A0.push_back({W, V});
    } else {
      A1.push_back({W, V});
    }
  }
  vector dp0(W + 1, (int)-1e9), dp1(W + 1, (int)-1e9);
  dp0[0] = 0, dp1[0] = 0;
  for (auto i : A0) {
    for (int j = W; j >= i[0]; j--) {
      if (dp0[j - i[0]] >= 0) chmax(dp0[j], dp0[j - i[0]] + i[1]);
    }
  }
  for (auto i : A1) {
    for (int j = W; j >= i[0]; j--) {
      if (dp1[j - i[0]] >= 0) chmax(dp1[j], dp1[j - i[0]] + i[1]);
    }
  }
  int ans = 0;
  for (int i = 0; i <= W; i++) {
    for (int j = 0; i + j <= W; j++) {
      if (abs(i - j) <= D && dp0[i] >= 0 && dp1[j] >= 0) {
        chmax(ans, dp0[i] + dp1[j]);
      }
    }
  }
  cout << ans << '\n';
}
0