結果

問題 No.417 チューリップバブル
ユーザー nanophoto12nanophoto12
提出日時 2021-05-29 15:06:33
言語 C++17
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 320 ms / 2,000 ms
コード長 2,028 bytes
コンパイル時間 2,135 ms
コンパイル使用メモリ 205,100 KB
実行使用メモリ 6,656 KB
最終ジャッジ日時 2024-04-25 11:44:29
合計ジャッジ時間 6,820 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 4 ms
5,376 KB
testcase_09 AC 8 ms
5,376 KB
testcase_10 AC 12 ms
5,376 KB
testcase_11 AC 46 ms
5,376 KB
testcase_12 AC 47 ms
5,376 KB
testcase_13 AC 20 ms
5,376 KB
testcase_14 AC 76 ms
5,376 KB
testcase_15 AC 7 ms
5,376 KB
testcase_16 AC 7 ms
5,376 KB
testcase_17 AC 42 ms
5,376 KB
testcase_18 AC 42 ms
5,376 KB
testcase_19 AC 40 ms
5,376 KB
testcase_20 AC 163 ms
5,376 KB
testcase_21 AC 157 ms
5,376 KB
testcase_22 AC 158 ms
5,376 KB
testcase_23 AC 154 ms
5,376 KB
testcase_24 AC 2 ms
5,376 KB
testcase_25 AC 156 ms
5,376 KB
testcase_26 AC 16 ms
5,376 KB
testcase_27 AC 105 ms
5,376 KB
testcase_28 AC 156 ms
5,376 KB
testcase_29 AC 160 ms
5,376 KB
testcase_30 AC 151 ms
5,376 KB
testcase_31 AC 150 ms
5,376 KB
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 6 ms
5,376 KB
testcase_34 AC 30 ms
5,376 KB
testcase_35 AC 306 ms
6,400 KB
testcase_36 AC 302 ms
6,400 KB
testcase_37 AC 320 ms
6,656 KB
testcase_38 AC 313 ms
6,400 KB
testcase_39 AC 306 ms
6,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define M_PI       3.14159265358979323846   // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;

#define rep(a,n) for(ll a = 0;a < n;a++)
#define repi(a,b,n) for(ll a = b;a < n;a++)

template<typename T>
void chmax(T& reference, T value) {
    reference = max(reference, value);
}

template<typename T>
void chmaxmap(map<T, T>& m, T key, T value) {
    if (m.count(key)) {
        chmax(m[key], value);
    }
    else {
        m[key] = value;
    }
}

template<typename T>
void chmin(T& reference, T value) {
    reference = min(reference, value);
}

int main() {
    int n, m;
    cin >> n >> m;
    vector<ll> us(n);
    rep(i, n) cin >> us[i];
    vector<vector<P>> g(n);
    rep(i, n - 1) {
        ll a, b, c;
        cin >> a >> b >> c;
        g[a].emplace_back(b, c);
        g[b].emplace_back(a, c);
    }
    vector<vector<ll>> dp(n, vector<ll>(m + 1, -1));
    //dp[i][j]...iの村でm時間消費して得られる税収の最大値
    function<void(int, int)> dfs = [&](int current, int parent) {
        dp[current][0] = 0;
        for (auto nx : g[current]) {
            if (nx.first == parent) continue;
            dfs(nx.first, current);
            ll time = nx.second * 2;
            for (int ij = m - time; ij >= 0; ij--) {
                for (int j = 0; j <= ij; j++) {
                    int i = ij - j;
                    if (dp[nx.first][j] == -1) continue;
                    if (dp[current][i] == -1) continue;
                    ll cost = dp[current][i] + dp[nx.first][j];
                    chmax(dp[current][ij + time], cost);
                }
            }
        }
        for (int i = 0; i <= m; i++) {
            if (dp[current][i] == -1) continue;
            dp[current][i] += us[current];
        }
    };
    dfs(0, -1);
    ll ans = 0;
    rep(i, m + 1) {
        chmax(ans, dp[0][i]);
    }
    cout << ans << endl;
    return 0;
}
0