結果

問題 No.417 チューリップバブル
ユーザー sekiya9311
提出日時 2018-03-31 07:51:33
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,205 ms / 2,000 ms
コード長 1,147 bytes
コンパイル時間 1,881 ms
コンパイル使用メモリ 171,448 KB
実行使用メモリ 7,272 KB
最終ジャッジ日時 2024-06-26 01:15:59
合計ジャッジ時間 16,726 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

const int MAX_N = 222;
const int MAX_M = 2222;

int N, M;
long long U[MAX_N];
vector<pair<int, int>> G[MAX_N];
long long dp[MAX_N][MAX_M];

void dfs(int now, int prev) {
    for (int i = 0; i <= M; i++) {
        dp[now][i] = U[now];
    }
    for (auto &&el : G[now]) {
        if (el.first != prev) {
            dfs(el.first, now);
            for (int i = M; i >= 0; i--) {
                for (int j = 0; j <= M; j++) {
                    if (i - j - el.second * 2 < 0) continue;
                    dp[now][i] = max(dp[now][i], dp[now][i - j - el.second * 2] + dp[el.first][j]);
                }
            }
        }
    }
}

int main() {
    scanf("%d%d", &N, &M);
    for (int i = 0; i < N; i++) {
        scanf("%lld", U + i);
    }
    for (int i = 0; i < N - 1; i++) {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        //a--;b--;
        G[a].emplace_back(make_pair(b, c));
        G[b].emplace_back(make_pair(a, c));
    }
    dfs(0, -1);
    long long ans = 0;
    for (int i = 0; i <= M; i++) {
        ans = max(ans, dp[0][i]);
    }
    printf("%lld\n", ans);
}
0