結果

問題 No.2741 Balanced Choice
ユーザー MZKiMZKi
提出日時 2024-04-21 14:07:10
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 228 ms / 2,000 ms
コード長 1,696 bytes
コンパイル時間 4,229 ms
コンパイル使用メモリ 257,048 KB
実行使用メモリ 199,040 KB
最終ジャッジ日時 2024-04-21 14:07:17
合計ジャッジ時間 6,722 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 197 ms
199,040 KB
testcase_01 AC 201 ms
198,912 KB
testcase_02 AC 200 ms
198,912 KB
testcase_03 AC 228 ms
198,912 KB
testcase_04 AC 205 ms
199,040 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 123 ms
129,792 KB
testcase_08 AC 174 ms
176,512 KB
testcase_09 AC 116 ms
123,264 KB
testcase_10 AC 133 ms
141,056 KB
testcase_11 AC 170 ms
177,792 KB
権限があれば一括ダウンロードができます

ソースコード

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