結果

問題 No.417 チューリップバブル
ユーザー ooaiu
提出日時 2024-11-29 14:22:11
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 33 ms / 2,000 ms
コード長 1,787 bytes
コンパイル時間 2,915 ms
コンパイル使用メモリ 258,416 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2024-11-29 14:22:23
合計ジャッジ時間 4,300 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#ifndef LOCAL
#include <bits/stdc++.h>

using namespace std;

#define debug(...) (void(0))
#else
#include "algo/debug.h"
#endif

namespace std {

template <class Fun>
class y_combinator_result {
    Fun fun_;

   public:
    template <class T>
    explicit y_combinator_result(T &&fun) : fun_(std::forward<T>(fun)) {}

    template <class... Args>
    decltype(auto) operator()(Args &&...args) {
        return fun_(std::ref(*this), std::forward<Args>(args)...);
    }
};

template <class Fun>
decltype(auto) y_combinator(Fun &&fun) {
    return y_combinator_result<std::decay_t<Fun>>(std::forward<Fun>(fun));
}

}  // namespace std

void solve() {
    int N, M;
    cin >> N >> M;
    vector<int> U(N);
    for (int i = 0; i < N; i++) cin >> U[i];
    vector<vector<pair<int, int>>> G(N);
    for (int i = 1; i < N; i++) {
        int a, b, c;
        cin >> a >> b >> c;
        G[a].push_back({b, c});
        G[b].push_back({a, c});
    }
    const int64_t linf = 1e18;
    vector<vector<int64_t>> dp(N, vector<int64_t>(M + 1, -linf));
    y_combinator([&](auto self, int v, int p) -> void {
        dp[v][0] = U[v];
        for (const auto &[to, cst] : G[v]) if(to != p) {
            self(to, v);
            for(int i = M; i >= 0; i--) if(dp[v][i] != -linf) {
                for(int j = 0; j <= M; j++) if(dp[to][j] != -linf){
                    int t = 2 * cst + j + i;
                    if(t <= M) dp[v][t] = max(dp[v][t], dp[to][j] + dp[v][i]);
                }
            }
        }
    })(0, -1);
    int64_t ans = -linf;
    for(int i = 0; i <= M; i++) ans = max(ans, dp[0][i]);
    cout << ans << endl;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int tt = 1;
    // std::cin >> tt;
    while (tt--) {
        solve();
    }
}
0