結果

問題 No.2741 Balanced Choice
ユーザー sibasyunsibasyun
提出日時 2024-04-23 21:28:49
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 77 ms / 2,000 ms
コード長 1,513 bytes
コンパイル時間 2,317 ms
コンパイル使用メモリ 201,212 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-04-23 21:28:56
合計ジャッジ時間 3,620 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
6,812 KB
testcase_01 AC 75 ms
6,940 KB
testcase_02 AC 76 ms
6,940 KB
testcase_03 AC 77 ms
6,940 KB
testcase_04 AC 77 ms
6,940 KB
testcase_05 AC 2 ms
6,944 KB
testcase_06 AC 2 ms
6,944 KB
testcase_07 AC 42 ms
6,940 KB
testcase_08 AC 62 ms
6,944 KB
testcase_09 AC 41 ms
6,944 KB
testcase_10 AC 47 ms
6,944 KB
testcase_11 AC 63 ms
6,940 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
// #include <atcoder/all>
#include <iostream>
#include <math.h>

using namespace std;
// using namespace atcoder;
// using mint = modint998244353;
// using mint = modint1000000007;
using vi = vector<int>;
using vvi = vector<vector<int>>;
using ll = long long;
template <class T> using max_heap = priority_queue<T>;
template <class T> using min_heap = priority_queue<T, vector<T>, greater<>>;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define rep2(i, f, n) for (int i = (int) f; i < (int)(n); i++)
#define repd(i, n, l) for (int i = (int) n; i >= (int) l; i--)

const ll inf = ll(1e9+7);

int N, W, D;

vector<ll> solve(vector<pair<int, int>> A){
    int n = A.size();
    vector<ll> dp(W+1, -inf);
    dp[0] = 0;
    rep(i, n){
        int w = A[i].first;
        int v = A[i].second;
        vector<ll> ndp(W+1, -inf);
        rep(j, W+1){
            ndp[j] = max(dp[j], ndp[j]);
            if (j + w <= W) ndp[j+w] = max(ndp[j+w], dp[j] + v);
        }
        swap(ndp, dp);
    }
    return dp;
}

int main() {
    cin >> N >> W >> D;
    vector<pair<int, int>> A;
    vector<pair<int, int>> B;
    rep(_, N){
        int t, w, v;
        cin >> t >> w >> v;
        if (t == 0) A.push_back({w, v});
        else B.push_back({w, v}); 
    }
    vector<ll> dp1 = solve(A);
    vector<ll> dp2 = solve(B);
    ll ans = 0;
    rep(j, W+1) rep(jj, W+1){
        if (j + jj <= W && abs(j-jj)<=D) ans = max(ans, dp1[j] + dp2[jj]);
    }
    cout << ans << endl;
    return 0;
}
0