結果

問題 No.1488 Max Score of the Tree
ユーザー aaaaaaaaaa2230aaaaaaaaaa2230
提出日時 2021-04-23 21:58:32
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 186 ms / 2,000 ms
コード長 766 bytes
コンパイル時間 279 ms
コンパイル使用メモリ 87,204 KB
実行使用メモリ 78,012 KB
最終ジャッジ日時 2023-09-17 12:10:43
合計ジャッジ時間 6,177 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 176 ms
77,772 KB
testcase_01 AC 175 ms
77,740 KB
testcase_02 AC 173 ms
77,884 KB
testcase_03 AC 183 ms
77,812 KB
testcase_04 AC 186 ms
77,784 KB
testcase_05 AC 96 ms
71,684 KB
testcase_06 AC 121 ms
77,320 KB
testcase_07 AC 144 ms
77,724 KB
testcase_08 AC 128 ms
77,268 KB
testcase_09 AC 127 ms
77,188 KB
testcase_10 AC 143 ms
77,472 KB
testcase_11 AC 178 ms
77,676 KB
testcase_12 AC 103 ms
76,940 KB
testcase_13 AC 122 ms
77,168 KB
testcase_14 AC 146 ms
77,724 KB
testcase_15 AC 131 ms
77,720 KB
testcase_16 AC 113 ms
77,520 KB
testcase_17 AC 123 ms
77,664 KB
testcase_18 AC 162 ms
77,644 KB
testcase_19 AC 135 ms
77,836 KB
testcase_20 AC 118 ms
77,280 KB
testcase_21 AC 116 ms
77,484 KB
testcase_22 AC 132 ms
77,380 KB
testcase_23 AC 96 ms
71,580 KB
testcase_24 AC 94 ms
71,556 KB
testcase_25 AC 95 ms
71,528 KB
testcase_26 AC 123 ms
77,808 KB
testcase_27 AC 110 ms
77,116 KB
testcase_28 AC 115 ms
77,200 KB
testcase_29 AC 119 ms
77,400 KB
testcase_30 AC 163 ms
78,012 KB
testcase_31 AC 182 ms
77,804 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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])
0