結果
| 問題 |
No.1488 Max Score of the Tree
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2021-04-23 21:58:32 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 136 ms / 2,000 ms |
| コード長 | 766 bytes |
| コンパイル時間 | 234 ms |
| コンパイル使用メモリ | 82,304 KB |
| 実行使用メモリ | 65,536 KB |
| 最終ジャッジ日時 | 2024-07-04 07:58:54 |
| 合計ジャッジ時間 | 3,918 ms |
|
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
from collections import deque
n,k = map(int,input().split())
e = [[] for i in range(n)]
cand = []
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))
cand.append((c,i))
count = [0]*n
def dfs(x,p,ind):
num = 0
leaf = 1
for nex,c,i in e[x]:
if nex == p:
continue
leaf = 0
num += dfs(nex,x,i)
if leaf:
num = 1
count[ind] += num
return num
dfs(0,-1,n-1)
base = 0
for i in range(n-1):
base += cand[i][0]*count[i]
dp = [0]*(k+1)
for i in range(n-1):
c = cand[i][0]
num = count[i]
for j in range(k+1)[::-1]:
if j+c <= k:
dp[j+c] = max(dp[j+c],dp[j]+num*c)
print(base+dp[-1])