結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
![]() |
提出日時 | 2021-04-23 22:27:19 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 119 ms / 2,000 ms |
コード長 | 884 bytes |
コンパイル時間 | 182 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 63,104 KB |
最終ジャッジ日時 | 2024-07-04 08:18:01 |
合計ジャッジ時間 | 3,400 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
from sys import stdinreadline = stdin.readlineN, 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 - 1links[a][b] = clinks[b][a] = cdef dfs1(n):l = links[n]for x in l:del links[x][n]for x in l:dfs1(x)dfs1(0)result = 0t = []def dfs2(n):global resultl = links[n]if len(l) == 0:return 1c = 0for x in l:a = dfs2(x)c += at.append((l[x], a))result += l[x] * areturn cdfs2(0)dp = [-1] * (K + 1)dp[0] = 0for i in range(len(t)):for j in range(K - 1, -1, -1):if dp[j] == -1:continuea = j + t[i][0]if a > K:continuedp[a] = max(dp[a], dp[j] + t[i][0] * t[i][1])result += max(dp)print(result)