結果

問題 No.2741 Balanced Choice
ユーザー おぼおぼ
提出日時 2024-04-21 11:00:17
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 45 ms / 2,000 ms
コード長 1,143 bytes
コンパイル時間 1,868 ms
コンパイル使用メモリ 167,176 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-21 11:00:44
合計ジャッジ時間 2,792 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
5,248 KB
testcase_01 AC 40 ms
5,248 KB
testcase_02 AC 45 ms
5,376 KB
testcase_03 AC 43 ms
5,376 KB
testcase_04 AC 41 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 17 ms
5,376 KB
testcase_08 AC 28 ms
5,376 KB
testcase_09 AC 19 ms
5,376 KB
testcase_10 AC 20 ms
5,376 KB
testcase_11 AC 29 ms
5,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define rep(i, a, x) for(int i = (a); i < (int)(x); i++)
#define all(x) (x).begin(), (x).end()
#define ll long long

int main() {
    int N, W, D;
    cin >> N >> W >> D;

    vector<int> type(N), weight(N);
    vector<ll> value(N);
    rep(i, 0, N) {
        cin >> type[i] >> weight[i] >> value[i];
    }

    const ll INF = 1e9 + 7; 
    vector<ll> dp0(W + 1, -INF), dp1(W + 1, -INF);
    dp0[0] = 0;
    dp1[0] = 0;

    rep(i, 0, N) {
        if (type[i] == 0) {
            for (int j = W; j >= weight[i]; j--) {
                dp0[j] = max(dp0[j], dp0[j - weight[i]] + value[i]);
            }
        } else {
            for (int j = W; j >= weight[i]; j--) {
                dp1[j] = max(dp1[j], dp1[j - weight[i]] + value[i]);
            }
        }
    }

    ll result = 0;
    rep(x, 0, W + 1) {
        rep(y, 0, W + 1) {
            if (x + y <= W && abs(x - y) <= D) {
                if (dp0[x] != -INF && dp1[y] != -INF) {
                    result = max(result, dp0[x] + dp1[y]);
                }
            }
        }
    }

    cout << result << endl;
    return 0;
}
0