#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() //#pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T& a, const T& b) { if (a < b) { a = b; return 1; } return 0; } templatebool chmin(T& a, const T& b) { if (b < a) { a = b; return 1; } return 0; } //--------------------------------------------------------------------------------------------------- /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i @hamayanhamayan     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, W, D; int t[5010], w[5010], v[5010]; const int BASE = 10101; int dp[5010][10010]; vector doDP(vector> wv) { int n = wv.size(); rep(i, 0, n + 1) rep(w, 0, W + 1) dp[i][w] = -inf; dp[0][0] = 0; rep(i, 0, n) rep(w, 0, W + 1) { chmax(dp[i + 1][w], dp[i][w]); chmax(dp[i + 1][w + wv[i].first], dp[i][w] + wv[i].second); } vector res(W + 1); rep(w, 0, W + 1) res[w] = dp[n][w]; return res; } void _main() { cin >> N >> W >> D; vector> wv[2]; rep(i, 0, N) { cin >> t[i] >> w[i] >> v[i]; wv[t[i]].push_back({w[i], v[i]}); } auto dp0 = doDP(wv[0]); auto dp1 = doDP(wv[1]); int ans = -inf; rep(w0, 0, W + 1) rep(w1, 0, W + 1) if(w0 + w1 <= W && abs(w0 - w1) <= D) chmax(ans, dp0[w0] + dp1[w1]); cout << ans << endl; } /* ## 前提知識 ## 解法 */