結果

問題 No.2741 Balanced Choice
ユーザー GOTKAKOGOTKAKO
提出日時 2024-04-20 10:45:43
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 74 ms / 2,000 ms
コード長 893 bytes
コンパイル時間 2,492 ms
コンパイル使用メモリ 200,056 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-20 10:45:48
合計ジャッジ時間 3,233 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 59 ms
5,248 KB
testcase_01 AC 63 ms
5,376 KB
testcase_02 AC 63 ms
5,376 KB
testcase_03 AC 66 ms
5,376 KB
testcase_04 AC 74 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 29 ms
5,376 KB
testcase_08 AC 48 ms
5,376 KB
testcase_09 AC 28 ms
5,376 KB
testcase_10 AC 34 ms
5,376 KB
testcase_11 AC 49 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr);
    
    int N,W,D; cin >> N >> W >> D;
    vector<pair<int,int>> Q0,Q1;
    for(int i=0; i<N; i++){
        int t,w,v; cin >> t >> w >> v;
        if(t == 0) Q0.push_back({w,v});
        if(t == 1) Q1.push_back({w,v});
    }

    int inf = 1e9;
    vector<int> dp0(W+1,-inf),dp1(W+1,-inf);
    dp0.at(0) = 0; dp1.at(0) = 0;
    for(auto [w,v] : Q0){
        for(int i=W; i>=0; i--) if(i+w <= W) dp0.at(i+w) = max(dp0.at(i+w),dp0.at(i)+v);
    } 
    for(auto [w,v] : Q1){
        for(int i=W; i>=0; i--) if(i+w <= W) dp1.at(i+w) = max(dp1.at(i+w),dp1.at(i)+v);
    }

    int answer = 0;
    for(int i=0; i<=W; i++) for(int k=0; k<=W; k++){
        if(i+k > W || abs(i-k) > D) continue;
        answer = max(answer,dp0.at(i)+dp1.at(k));
    }
    cout << answer << endl;
}
0