結果

問題 No.2741 Balanced Choice
ユーザー tnakao0123tnakao0123
提出日時 2024-05-01 10:39:33
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 39 ms / 2,000 ms
コード長 1,301 bytes
コンパイル時間 553 ms
コンパイル使用メモリ 55,276 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-05-01 10:39:34
合計ジャッジ時間 1,517 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 2741.cc:  No.2741 Balanced Choice - yukicoder
 */

#include<cstdio>
#include<vector>
#include<algorithm>
 
using namespace std;

/* constant */

const int MAX_N = 5000;
const int MAX_W = 5000;

/* typedef */

typedef vector<int> vi;

/* global variables */

int dp[2][MAX_W + 1];

/* subroutines */

inline void setmax(int &a, int b) { if (a < b) a = b; }

/* main */

int main() {
  int n, w, d;
  scanf("%d%d%d", &n, &w, &d);

  vi wvs[2], vvs[2];
  for (int i = 0; i < n; i++) {
    int ti, wi, vi;
    scanf("%d%d%d", &ti, &wi, &vi);
    wvs[ti].push_back(wi);
    vvs[ti].push_back(vi);
  }

  for (int t = 0; t < 2; t++) {
    fill(dp[t], dp[t] + w + 1, -1);
    dp[t][0] = 0;

    for (int i = 0; i < wvs[t].size(); i++) {
      int wti = wvs[t][i], vti = vvs[t][i];
      for (int j = w - wti; j >= 0; j--)
	if (dp[t][j] >= 0) setmax(dp[t][j + wti], dp[t][j] + vti);
    }

    //for (int j = 0; j <= w; j++) printf(" %d", dp[t][j]); putchar('\n');
  }

  int maxv = 0;
  for (int i = 0; i <= w; i++)
    for (int j = max(0, i - d); i + j <= w && j <= i + d; j++)
      if (dp[0][i] >= 0 && dp[1][j] >= 0) {
	//printf(" dp[0][%d]=%d, dp[1][%d]=%d\n", i, dp[0][i], j, dp[1][j]);
	setmax(maxv, dp[0][i] + dp[1][j]);
      }

  printf("%d\n", maxv);

  return 0;
}
0