結果
問題 | No.1488 Max Score of the Tree |
ユーザー |
|
提出日時 | 2021-12-28 21:07:55 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 254 ms / 2,000 ms |
コード長 | 920 bytes |
コンパイル時間 | 255 ms |
コンパイル使用メモリ | 82,304 KB |
実行使用メモリ | 144,256 KB |
最終ジャッジ日時 | 2024-10-02 15:19:01 |
合計ジャッジ時間 | 5,989 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 29 |
ソースコード
N,K = map(int,input().split())G = [[] for _ in range(N + 1)]for _ in range(N - 1):a,b,c = map(int,input().split())G[a].append((b,c))G[b].append((a,c))node = [[0] * 2 for _ in range(N + 1)]edge = []score = 0def tansaku(now = 1,parent = 0):global scorenode[now][0] = parentif now != 1 and len(G[now]) == 1:node[now][1] = 1return 1for v,c in G[now]:if v != parent:t = tansaku(v,now)node[now][1] += tedge.append((c,t))score += c * treturn node[now][1]tansaku()dp = [[0] * (K + 1) for _ in range(N-1)]dp[0][edge[0][0]] = edge[0][1] * edge[0][0]for i in range(1,N-1):c,t = edge[i]for j in range(K + 1):dp[i][j] = dp[i-1][j]if j - c >= 0:if dp[i][j] < dp[i-1][j-c] + c * t:dp[i][j] = dp[i-1][j-c] + c * tm = max(dp[N-2])print(m + score)