結果

問題 No.1488 Max Score of the Tree
コンテスト
ユーザー c-yan
提出日時 2021-04-24 01:49:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 122 ms / 2,000 ms
コード長 948 bytes
コンパイル時間 278 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 63,104 KB
最終ジャッジ日時 2024-07-04 08:53:22
合計ジャッジ時間 3,259 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

from sys import stdin

readline = stdin.readline

N, K = map(int, readline().split())
links = [{} for _ in range(N)]
for _ in range(N - 1):
    a, b, c = map(int, readline().split())
    a, b = a - 1, b - 1
    links[a][b] = c
    links[b][a] = c


def dfs(parent):
    l = links[parent]
    if len(l) == 0:
        return (0, 1)

    total_score = 0
    total_leaves = 0
    for child in l:
        del links[child][parent]
        score, leaves = dfs(child)
        total_score += score
        total_leaves += leaves
        t.append((l[child], leaves))
        total_score += l[child] * leaves
    return total_score, total_leaves


t = []
result, _ = dfs(0)

dp = [-1] * (K + 1)
dp[0] = 0
for i in range(len(t)):
    for j in range(K - 1,  -1, -1):
        if dp[j] == -1:
            continue
        a = j + t[i][0]
        if a > K:
            continue
        dp[a] = max(dp[a], dp[j] + t[i][0] * t[i][1])
result += max(dp)
print(result)
0