結果

問題 No.1488 Max Score of the Tree
ユーザー ntuda
提出日時 2025-07-05 14:26:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 102 ms / 2,000 ms
コード長 707 bytes
コンパイル時間 449 ms
コンパイル使用メモリ 82,184 KB
実行使用メモリ 76,512 KB
最終ジャッジ日時 2025-07-05 14:26:56
合計ジャッジ時間 3,802 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #

N, K = map(int, input().split())
E = [[] for _ in range(N)]
C = [[] for _ in range(N - 1)]

for i in range(N - 1):
    a, b, c = map(int, input().split())
    a -= 1
    b -= 1
    E[a].append((b, c, i))
    E[b].append((a, c, i))

def dfs(x, p=-1):
    f = True
    ret = 0
    for y, c, id in E[x]:
        if y != p:
            f = False
            a = dfs(y, x)
            C[id] = [a, c, id]
            ret += a
    if f:
        return 1
    return ret

dfs(0)
base = 0
for a, c, id in C:
    base += a * c

dp = [-1] * (K + 1)
dp[0] = 0
for a, c, id in C:
    for i in reversed(range(K - c + 1)):
        if dp[i] >= 0:
            dp[i + c] = max(dp[i + c], dp[i] + a * c)

print(base + max(dp))
0