結果

問題 No.1488 Max Score of the Tree
ユーザー convexineqconvexineq
提出日時 2021-04-23 21:46:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 132 ms / 2,000 ms
コード長 689 bytes
コンパイル時間 926 ms
コンパイル使用メモリ 87,064 KB
実行使用メモリ 77,092 KB
最終ジャッジ日時 2023-09-17 12:01:18
合計ジャッジ時間 4,809 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 116 ms
76,988 KB
testcase_01 AC 121 ms
76,820 KB
testcase_02 AC 115 ms
76,936 KB
testcase_03 AC 127 ms
76,960 KB
testcase_04 AC 126 ms
76,824 KB
testcase_05 AC 76 ms
71,280 KB
testcase_06 AC 91 ms
76,444 KB
testcase_07 AC 109 ms
76,632 KB
testcase_08 AC 92 ms
76,556 KB
testcase_09 AC 96 ms
76,136 KB
testcase_10 AC 107 ms
76,684 KB
testcase_11 AC 131 ms
77,072 KB
testcase_12 AC 81 ms
76,020 KB
testcase_13 AC 94 ms
76,056 KB
testcase_14 AC 106 ms
76,456 KB
testcase_15 AC 97 ms
76,736 KB
testcase_16 AC 85 ms
76,524 KB
testcase_17 AC 93 ms
76,532 KB
testcase_18 AC 112 ms
76,676 KB
testcase_19 AC 100 ms
76,756 KB
testcase_20 AC 87 ms
76,496 KB
testcase_21 AC 86 ms
76,696 KB
testcase_22 AC 93 ms
76,624 KB
testcase_23 AC 75 ms
71,020 KB
testcase_24 AC 74 ms
71,396 KB
testcase_25 AC 74 ms
71,424 KB
testcase_26 AC 91 ms
76,636 KB
testcase_27 AC 86 ms
76,040 KB
testcase_28 AC 86 ms
75,988 KB
testcase_29 AC 90 ms
76,368 KB
testcase_30 AC 117 ms
77,092 KB
testcase_31 AC 132 ms
76,864 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

n,K = map(int,input().split())
g = [[] for _ in range(n)]
for _ in range(n-1):
    a,b,c = map(int,input().split())
    g[a-1].append((b-1,c))
    g[b-1].append((a-1,c))
ans = 0

order = []
wt = [0]*n
st = [0]
parent = [-1]*n
while st:
    v = st.pop()
    order.append(v)
    for c,d in g[v]:
        if c != parent[v]:
            st.append(c)
            parent[c] = v
            wt[c] = d

res = []
size = [0]*n
for v in order[1:][::-1]:
    if len(g[v])==1: size[v] = 1
    ans += size[v]*wt[v]
    res.append((size[v],wt[v]))
    size[parent[v]] += size[v]

dp = [0]*(K+1)
for v,w in res:
    for i in range(w,K+1)[::-1]:
        dp[i] = max(dp[i], dp[i-w] + v*w)
print(ans+max(dp))
0