結果
| 問題 | No.1488 Max Score of the Tree | 
| コンテスト | |
| ユーザー |  mkawa2 | 
| 提出日時 | 2021-04-23 22:41:43 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,480 ms / 2,000 ms | 
| コード長 | 1,233 bytes | 
| コンパイル時間 | 239 ms | 
| コンパイル使用メモリ | 12,928 KB | 
| 実行使用メモリ | 14,080 KB | 
| 最終ジャッジ日時 | 2024-07-04 08:26:04 | 
| 合計ジャッジ時間 | 15,478 ms | 
| ジャッジサーバーID (参考情報) | judge5 / judge2 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 29 | 
ソースコード
import sys
sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.buffer.readline())
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LI1(): return list(map(int1, sys.stdin.buffer.readline().split()))
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
def BI(): return sys.stdin.buffer.readline().rstrip()
def SI(): return sys.stdin.buffer.readline().rstrip().decode()
inf = 10 ** 16
md = 10 ** 9 + 7
# md = 998244353
n,k=LI()
to=[[] for _ in range(n)]
cc=[]
for i in range(n-1):
    u,v,c=LI()
    u,v=u-1,v-1
    to[u].append((v,c,i))
    to[v].append((u,c,i))
    cc.append(c)
leaf=[0]*n
etou=[0]*n
def dfs(u=0,pu=-1,s=0):
    if u and len(to[u])==1:
        leaf[u]=1
        return s
    res=0
    for v,c,e in to[u]:
        if v==pu:continue
        etou[e]=v
        res+=dfs(v,u,s+c)
        leaf[u]+=leaf[v]
    return res
score=dfs()
dp=[-1]*(k+1)
dp[0]=0
for e in range(n-1):
    c=cc[e]
    u=etou[e]
    for i in range(k-c,-1,-1):
        if dp[i]==-1:continue
        ni=i+c
        dp[ni]=max(dp[ni],dp[i]+c*leaf[u])
print(score+max(dp))
            
            
            
        