結果

問題 No.1488 Max Score of the Tree
ユーザー AEnAEn
提出日時 2022-12-13 00:03:47
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 244 ms / 2,000 ms
コード長 977 bytes
コンパイル時間 326 ms
コンパイル使用メモリ 82,480 KB
実行使用メモリ 143,360 KB
最終ジャッジ日時 2024-04-24 13:27:47
合計ジャッジ時間 5,436 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 226 ms
139,136 KB
testcase_01 AC 227 ms
135,936 KB
testcase_02 AC 232 ms
140,416 KB
testcase_03 AC 244 ms
142,976 KB
testcase_04 AC 236 ms
142,592 KB
testcase_05 AC 38 ms
52,736 KB
testcase_06 AC 97 ms
83,584 KB
testcase_07 AC 162 ms
108,672 KB
testcase_08 AC 121 ms
95,360 KB
testcase_09 AC 107 ms
86,528 KB
testcase_10 AC 172 ms
111,104 KB
testcase_11 AC 238 ms
142,848 KB
testcase_12 AC 46 ms
59,776 KB
testcase_13 AC 77 ms
75,648 KB
testcase_14 AC 145 ms
101,376 KB
testcase_15 AC 110 ms
87,168 KB
testcase_16 AC 62 ms
68,096 KB
testcase_17 AC 86 ms
78,208 KB
testcase_18 AC 183 ms
116,224 KB
testcase_19 AC 118 ms
92,672 KB
testcase_20 AC 77 ms
75,008 KB
testcase_21 AC 66 ms
69,632 KB
testcase_22 AC 105 ms
86,912 KB
testcase_23 AC 36 ms
52,352 KB
testcase_24 AC 38 ms
52,264 KB
testcase_25 AC 39 ms
52,352 KB
testcase_26 AC 93 ms
81,280 KB
testcase_27 AC 56 ms
65,920 KB
testcase_28 AC 74 ms
72,576 KB
testcase_29 AC 83 ms
75,252 KB
testcase_30 AC 199 ms
127,104 KB
testcase_31 AC 234 ms
143,360 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
sys.setrecursionlimit(10**8)
import pypyjit
pypyjit.set_param("max_unroll_recursion=-1")
input = sys.stdin.readline

N, K = map(int, input().split())
G = [list() for _ in range(N)]
for i 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))

leaf = [0]*N
A = []
score = 0
def dfs(v, p):
    for next_v,_ in G[v]:
        if next_v == p:
            continue
        dfs(next_v, v)
    if len(G[v])==1 and v!=0:
        leaf[v] = 1
    else:
        leaf[v] = 0
        for c,cost in G[v]:
            if c== p:
                continue
            leaf[v] += leaf[c]
            A.append([cost,leaf[c]*cost])
            global score
            score += cost*leaf[c]

dfs(0,-1)
dp = [0]*(K+1)
for i in range(N-1):
    ndp = [0]*(K+1)
    w, v = A[i]
    for j in range(K+1):
        ndp[j] = max(ndp[j],dp[j])
        if j-w>=0:
            ndp[j] = max(ndp[j],dp[j-w]+v)
    dp = ndp
print(score+dp[-1])
0