結果

問題 No.2741 Balanced Choice
ユーザー Series_205Series_205
提出日時 2024-04-20 11:35:20
言語 C++17
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 342 ms / 2,000 ms
コード長 1,911 bytes
コンパイル時間 2,360 ms
コンパイル使用メモリ 204,028 KB
実行使用メモリ 297,088 KB
最終ジャッジ日時 2024-04-20 11:35:27
合計ジャッジ時間 5,985 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 289 ms
296,960 KB
testcase_01 AC 292 ms
297,088 KB
testcase_02 AC 289 ms
296,960 KB
testcase_03 AC 315 ms
296,960 KB
testcase_04 AC 342 ms
296,960 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 1 ms
5,376 KB
testcase_07 AC 183 ms
192,768 KB
testcase_08 AC 250 ms
263,296 KB
testcase_09 AC 171 ms
183,168 KB
testcase_10 AC 222 ms
209,664 KB
testcase_11 AC 254 ms
265,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define FOR(i, l, r) for(ll i = l; i < r; i++)
#define rep(i, n) FOR(i, 0, n)
#define FORR(i, l, r) for(ll i = r - 1; i >= l; i--)
#define ALL(ar) begin(ar), end(ar)

template <typename T1, typename T2>
inline bool chmax(T1 &a, T2 b) {
    return a < b && (a = b, true);
}
template <typename T1, typename T2>
inline bool chmin(T1 &a, T2 b) {
    return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
    is >> p.first >> p.second;
    return is;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
    os << p.first << ' ' << p.second;
    return os;
}
template <typename T>
istream &operator>>(istream &is, vector<T> &v) {
    for(T &x : v) is >> x;
    return is;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    for(size_t i = 0; i < v.size(); i++) os << (i ? " " : "") << v[i];
    return os;
}
//-------------------------------------------------

void solve() {
    int N, W, D;
    cin >> N >> W >> D;
    vector<int> w[2], v[2];
    rep(i, N) {
        int a, b, c;
        cin >> a >> b >> c;
        w[a].push_back(b);
        v[a].push_back(c);
    }

    vector dp(2, vector(N + 1, vector<int>(W + 1, -(1 << 29))));
    dp[0][0][0] = dp[1][0][0] = 0;
    rep(t, 2) {
        rep(i, w[t].size()) {
            rep(j, W) if(dp[t][i][j] >= 0) {
                chmax(dp[t][i + 1][j], dp[t][i][j]);
                if(j + w[t][i] <= W)
                    chmax(dp[t][i + 1][j + w[t][i]], dp[t][i][j] + v[t][i]);
            }
        }
    }

    int mx = 0;
    rep(i, W + 1) rep(j, W + 1) if(i + j <= W && abs(i - j) <= D)
        chmax(mx, dp[0][w[0].size()][i] + dp[1][w[1].size()][j]);
    cout << mx << endl;
}

int main() {
    int t = 1;
    // cin >> t;
    while(t--) solve();
}
0