結果

問題 No.1488 Max Score of the Tree
ユーザー titiatitia
提出日時 2021-04-23 23:25:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 163 ms / 2,000 ms
コード長 803 bytes
コンパイル時間 1,282 ms
コンパイル使用メモリ 86,668 KB
実行使用メモリ 76,784 KB
最終ジャッジ日時 2023-09-17 13:10:33
合計ジャッジ時間 6,164 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 148 ms
76,696 KB
testcase_01 AC 151 ms
76,684 KB
testcase_02 AC 158 ms
76,784 KB
testcase_03 AC 155 ms
76,596 KB
testcase_04 AC 159 ms
76,636 KB
testcase_05 AC 75 ms
71,156 KB
testcase_06 AC 98 ms
76,460 KB
testcase_07 AC 123 ms
76,568 KB
testcase_08 AC 108 ms
76,588 KB
testcase_09 AC 103 ms
76,208 KB
testcase_10 AC 121 ms
76,624 KB
testcase_11 AC 152 ms
76,640 KB
testcase_12 AC 79 ms
76,144 KB
testcase_13 AC 95 ms
75,908 KB
testcase_14 AC 118 ms
76,444 KB
testcase_15 AC 104 ms
76,188 KB
testcase_16 AC 86 ms
76,308 KB
testcase_17 AC 95 ms
76,396 KB
testcase_18 AC 129 ms
76,396 KB
testcase_19 AC 109 ms
76,420 KB
testcase_20 AC 94 ms
76,124 KB
testcase_21 AC 85 ms
76,120 KB
testcase_22 AC 104 ms
76,320 KB
testcase_23 AC 74 ms
71,080 KB
testcase_24 AC 74 ms
71,316 KB
testcase_25 AC 75 ms
71,096 KB
testcase_26 AC 97 ms
76,512 KB
testcase_27 AC 83 ms
75,856 KB
testcase_28 AC 89 ms
76,056 KB
testcase_29 AC 94 ms
76,192 KB
testcase_30 AC 139 ms
76,664 KB
testcase_31 AC 163 ms
76,580 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,K=map(int,input().split())

E=[tuple(map(int,input().split())) for i in range(N-1)]

EDGE=[[] for i in range(N)]

for a,b,e in E:
    a-=1
    b-=1
    EDGE[a].append((b,e))
    EDGE[b].append((a,e))

QUE=[0] 
Parent=[-1]*(N+1)
Parent[0]=0
TOP_SORT=[]

while QUE:
    x=QUE.pop()
    TOP_SORT.append(x)
    for to,w in EDGE[x]:
        if Parent[to]==-1:
            Parent[to]=x,w
            QUE.append(to)

EW=[0]*N

for n in TOP_SORT[1:][::-1]:
    if EW[n]==0:
        EW[n]=1
    EW[Parent[n][0]]+=EW[n]

XX=[]
for i in range(1,N):
    XX.append((Parent[i][1],EW[i]))

ANS=0

for x,c in XX:
    ANS+=x*c

cost=0
DP=[0]*(K+1)

for x,c in XX:
    for j in range(K,-1,-1):
        if j+x<=K:
            DP[j+x]=max(DP[j+x],DP[j]+x*c)
print(max(DP)+ANS)
    
0