結果

問題 No.1488 Max Score of the Tree
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2021-04-23 21:45:38
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 130 ms / 2,000 ms
コード長 811 bytes
コンパイル時間 284 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 76,940 KB
最終ジャッジ日時 2023-09-17 11:59:19
合計ジャッジ時間 5,017 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 119 ms
76,612 KB
testcase_01 AC 123 ms
76,712 KB
testcase_02 AC 114 ms
76,664 KB
testcase_03 AC 125 ms
76,844 KB
testcase_04 AC 129 ms
76,380 KB
testcase_05 AC 77 ms
71,012 KB
testcase_06 AC 95 ms
76,580 KB
testcase_07 AC 109 ms
76,680 KB
testcase_08 AC 93 ms
76,572 KB
testcase_09 AC 96 ms
76,260 KB
testcase_10 AC 106 ms
76,444 KB
testcase_11 AC 127 ms
76,940 KB
testcase_12 AC 80 ms
76,004 KB
testcase_13 AC 91 ms
76,028 KB
testcase_14 AC 103 ms
76,556 KB
testcase_15 AC 95 ms
76,480 KB
testcase_16 AC 84 ms
76,228 KB
testcase_17 AC 90 ms
76,432 KB
testcase_18 AC 109 ms
76,492 KB
testcase_19 AC 97 ms
76,736 KB
testcase_20 AC 88 ms
76,236 KB
testcase_21 AC 86 ms
76,204 KB
testcase_22 AC 94 ms
76,244 KB
testcase_23 AC 74 ms
71,012 KB
testcase_24 AC 75 ms
71,172 KB
testcase_25 AC 75 ms
71,060 KB
testcase_26 AC 90 ms
76,588 KB
testcase_27 AC 84 ms
75,736 KB
testcase_28 AC 85 ms
76,044 KB
testcase_29 AC 90 ms
76,252 KB
testcase_30 AC 114 ms
76,740 KB
testcase_31 AC 130 ms
76,516 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

ナップザック問題やな
辺が99個しかない



"""

import sys
from sys import stdin
import math

ans = 0
elis = []

def dfs(v,p):

    global ans
    global elis

    vsum = 0
    if v != p and len(lis[v]) == 1:
        return 1
    
    for nex,cost in lis[v]:
        if nex != p:

            cat = dfs(nex,v)
            elis.append( (cost,cost*cat) )
            vsum += cat
            ans += cost * cat

    return vsum

N,K = map(int,stdin.readline().split())

lis = [ [] for i in range(N)]
for i in range(N-1):

    a,b,c = map(int,stdin.readline().split())
    a -= 1
    b -= 1

    lis[a].append( (b,c) )
    lis[b].append( (a,c) )

dfs(0,0)

dp = [0] * (K+1)
for cost,r in elis:
    for i in range(K-cost,-1,-1):
        dp[i+cost] = max(dp[i+cost],dp[i]+r)

print (ans + max(dp))
0