結果

問題 No.1488 Max Score of the Tree
ユーザー monnumonnu
提出日時 2021-04-23 23:31:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 135 ms / 2,000 ms
コード長 614 bytes
コンパイル時間 1,659 ms
コンパイル使用メモリ 86,664 KB
実行使用メモリ 76,936 KB
最終ジャッジ日時 2023-09-17 13:12:46
合計ジャッジ時間 4,726 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 113 ms
76,828 KB
testcase_01 AC 128 ms
76,548 KB
testcase_02 AC 135 ms
76,828 KB
testcase_03 AC 131 ms
76,600 KB
testcase_04 AC 118 ms
76,552 KB
testcase_05 AC 73 ms
71,132 KB
testcase_06 AC 92 ms
76,348 KB
testcase_07 AC 104 ms
76,648 KB
testcase_08 AC 93 ms
76,684 KB
testcase_09 AC 96 ms
75,908 KB
testcase_10 AC 102 ms
76,508 KB
testcase_11 AC 124 ms
76,612 KB
testcase_12 AC 78 ms
75,888 KB
testcase_13 AC 92 ms
75,920 KB
testcase_14 AC 104 ms
76,508 KB
testcase_15 AC 95 ms
76,348 KB
testcase_16 AC 87 ms
76,300 KB
testcase_17 AC 94 ms
76,588 KB
testcase_18 AC 114 ms
76,652 KB
testcase_19 AC 102 ms
76,540 KB
testcase_20 AC 90 ms
76,004 KB
testcase_21 AC 88 ms
76,388 KB
testcase_22 AC 99 ms
76,396 KB
testcase_23 AC 72 ms
71,220 KB
testcase_24 AC 73 ms
71,288 KB
testcase_25 AC 74 ms
71,276 KB
testcase_26 AC 91 ms
76,916 KB
testcase_27 AC 89 ms
76,132 KB
testcase_28 AC 89 ms
76,188 KB
testcase_29 AC 90 ms
76,244 KB
testcase_30 AC 112 ms
76,936 KB
testcase_31 AC 129 ms
76,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

def dfs(G,v,p,sz,edge):
    cnt=0
    for nv,c in G[v]:
        if nv==p:
            continue
        cnt+=1
        dfs(G,nv,v,sz,edge)
        sz[v]+=sz[nv]
        edge.append([c,sz[nv]])
    if cnt==0:
        sz[v]=1


N,K=map(int,input().split())
G=[[] for i in range(N)]
for _ in range(N-1):
    a,b,c=map(int,input().split())
    a-=1
    b-=1
    G[a].append([b,c])
    G[b].append([a,c])

sz=[0]*N
edge=[]
dfs(G,0,-1,sz,edge)
ans=0
dp=[0]*(K+1)
for e,cnt in edge:
    ans+=e*cnt
    for i in range(K,-1,-1):
        if i-e<0:
            break;
        dp[i]=max(dp[i],dp[i-e]+cnt*e)
print(ans+max(dp))
0