/* -*- coding: utf-8 -*- * * 2741.cc: No.2741 Balanced Choice - yukicoder */ #include #include #include using namespace std; /* constant */ const int MAX_N = 5000; const int MAX_W = 5000; /* typedef */ typedef vector 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; }