結果
| 問題 | No.1488 Max Score of the Tree | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2021-04-23 22:41:55 | 
| 言語 | C++17 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                WA
                                 
                             | 
| 実行時間 | - | 
| コード長 | 2,174 bytes | 
| コンパイル時間 | 2,544 ms | 
| コンパイル使用メモリ | 204,300 KB | 
| 最終ジャッジ日時 | 2025-01-21 00:14:15 | 
| ジャッジサーバーID (参考情報) | judge5 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 8 WA * 21 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
using int128 = __int128_t;
#define rep(i, n)         for (int i = 0; i < n; ++i)
#define reps(i, n, s)     for (int i = 0; i < n; i += s)
#define repl(i, l, r)     for (int i = l; i < r; ++i)
#define repsl(i, l, r, s) for (int i = l; i < r; i += s)
#define all(iterable) (iterable).begin(), (iterable).end()
constexpr int dx4[4] = {1, 0, -1, 0};
constexpr int dy4[4] = {0, 1, 0, -1};
constexpr int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1};
constexpr int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1};
template <typename T>
void print(const vector<T>& vec, const string sep = " ", const string end = "\n") {
    int n = vec.size();
    rep(i, n) {
        cout << vec[i];
        if (i < n - 1) cout << sep;
        else cout << end;
    }
}
template <typename T>
void read(vector<T>& a, int begin_index, int length) {
    if (a.size() < begin_index + length) a.resize(begin_index + length);
    while (length --> 0) cin >> a[begin_index++];
}
template <typename T>
void read(vector<T>& a) { read(a, 0, a.size()); }
using tree = vector<vector<pair<int, int>>>;
int main() {
    int n, k;
    cin >> n >> k;
    tree t(n);
    rep(i, n - 1) {
        int u, v, c;
        cin >> u >> v >> c;
        --u, --v;
        t[u].push_back({v, c});
        t[v].push_back({u, c});
    }
    vector<int> pst, par(n), sub(n, 0), val(n, 0);
    pst.reserve(n);
    auto dfs = [&](auto self, int u, int p) -> void {
        par[u] = p;
        for (auto &[v, c] : t[u]) {
            if (v == p) {
                val[u] = c;
                continue;
            }
            self(self, v, u);
        }
        pst.push_back(u);
    };
    dfs(dfs, 0, -1);
    long long ans = 0;
    for (int u : pst) {
        for (auto &[v, c] : t[u]) {
            sub[u] += sub[v];
        }
        if (sub[u] == 0) sub[u] = 1;
        ans += val[u] * sub[u];
    }
    vector<long long> dp(k + 1, 0);
    repl(i, 1, n) {
        long long scr = (long long) val[i] * sub[i];
        repl(j, val[i], k + 1) {
            dp[j] = max(dp[j], dp[j - val[i]] + scr);
        }
    }
    ans += *max_element(all(dp));
    cout << ans << '\n';
    return 0;
}
            
            
            
        