結果

問題 No.2741 Balanced Choice
ユーザー MZKi
提出日時 2024-04-21 14:07:10
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 231 ms / 2,000 ms
コード長 1,696 bytes
コンパイル時間 4,691 ms
コンパイル使用メモリ 253,892 KB
最終ジャッジ日時 2025-02-21 08:09:49
ジャッジサーバーID
(参考情報)
judge2 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 10
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<atcoder/all>
using namespace atcoder;

#include <bits/stdc++.h>
template<class T> inline bool chmin(T&a, T b){if(a > b){a = b; return true;}else{return false;}}
template<class T> inline bool chmax(T&a, T b){if(a < b){a = b; return true;}else{return false;}}
#define ll long long
#define double long double
#define rep(i,n) for(int i=0;i<(n);i++)
#define REP(i,n) for(int i=1;i<=(n);i++)
#define mod (ll)(1e9+7)
#define inf (ll)(3e18+7)
#define eps (double)(1e-9)
#define pi (double) acos(-1)
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
using namespace std;
 
int main() {
    int N, W, D;
    cin >> N >> W >> D;
    vector<int> t(N), w(N), v(N);
    rep(i, N)cin >> t[i] >> w[i] >> v[i];

    vector<int> w0, v0;
    vector<int> w1, v1;
    rep(i, N)if(t[i] == 0)w0.push_back(w[i]);
    rep(i, N)if(t[i] == 0)v0.push_back(v[i]);
    rep(i, N)if(t[i] == 1)w1.push_back(w[i]);
    rep(i, N)if(t[i] == 1)v1.push_back(v[i]);
    
    int n0 = w0.size();
    int n1 = w1.size();
    vector<vector<ll>> dp0(n0+1, vector<ll>(W+1, -inf));
    vector<vector<ll>> dp1(n1+1, vector<ll>(W+1, -inf));
    dp0[0][0] = 0;
    dp1[0][0] = 0;

    rep(i, n0){
        rep(j, W+1){
            chmax(dp0[i+1][j], dp0[i][j]);
            if(j + w0[i] <= W)chmax(dp0[i+1][j+w0[i]], dp0[i][j] + v0[i]);
        }
    }
    rep(i, n1){
        rep(j, W+1){
            chmax(dp1[i+1][j], dp1[i][j]);
            if(j + w1[i] <= W)chmax(dp1[i+1][j+w1[i]], dp1[i][j] + v1[i]);
        }
    }

    ll ans = -inf;
    rep(i, W+1)rep(j, W+1){
        if(abs(i-j) > D)continue;
        if(i + j > W)continue;
        chmax(ans, dp0[n0][i] + dp1[n1][j]);
    }

    cout << ans << endl;
}
0