結果
| 問題 | No.1488 Max Score of the Tree | 
| コンテスト | |
| ユーザー |  c-yan | 
| 提出日時 | 2021-04-24 01:23:14 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                TLE
                                 
                             | 
| 実行時間 | - | 
| コード長 | 942 bytes | 
| コンパイル時間 | 235 ms | 
| コンパイル使用メモリ | 12,800 KB | 
| 実行使用メモリ | 13,952 KB | 
| 最終ジャッジ日時 | 2024-07-04 08:53:10 | 
| 合計ジャッジ時間 | 25,781 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge3 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 26 TLE * 3 | 
ソースコード
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)
            
            
            
        