結果

問題 No.1488 Max Score of the Tree
ユーザー ntuda
提出日時 2025-07-05 14:19:44
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 662 bytes
コンパイル時間 444 ms
コンパイル使用メモリ 82,176 KB
実行使用メモリ 54,740 KB
最終ジャッジ日時 2025-07-05 14:19:47
合計ジャッジ時間 3,383 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 20
権限があれば一括ダウンロードができます

ソースコード

diff #

N,K = map(int,input().split())
E = [[] for _ in range(N)]
AB = []
L = []
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))
    AB.append((a,b))

C = [[] for _ in range(N-1)] #子孫に葉が幾つあるか
def dfs(x,p= -1):
    f = True
    ret = 0
    for y,c,id in E[x]:
        if y != p:
            f = False
            a = dfs(y,x)
            C[id] = [a,c,id]
            ret += a
    if f:
        return 1
    return ret


dfs(0)
C.sort(key = lambda x:(-x[0],-x[1]))
ans = 0
for a,c,id in C:
    ans += a * c
    if K >= c:
        K -= c
        ans += a * c
print(ans)

0