結果

問題 No.1488 Max Score of the Tree
ユーザー tktk_snsntktk_snsn
提出日時 2021-04-23 22:14:53
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 883 bytes
コンパイル時間 280 ms
コンパイル使用メモリ 87,000 KB
実行使用メモリ 77,648 KB
最終ジャッジ日時 2023-09-17 12:26:04
合計ジャッジ時間 4,914 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
77,340 KB
testcase_01 AC 133 ms
77,448 KB
testcase_02 AC 137 ms
77,584 KB
testcase_03 AC 135 ms
77,344 KB
testcase_04 AC 140 ms
77,456 KB
testcase_05 AC 76 ms
71,224 KB
testcase_06 AC 95 ms
76,960 KB
testcase_07 AC 114 ms
77,336 KB
testcase_08 AC 101 ms
77,280 KB
testcase_09 AC 99 ms
76,896 KB
testcase_10 AC 108 ms
77,320 KB
testcase_11 AC 136 ms
77,648 KB
testcase_12 AC 77 ms
76,276 KB
testcase_13 AC 91 ms
76,760 KB
testcase_14 AC 106 ms
77,228 KB
testcase_15 AC 97 ms
77,068 KB
testcase_16 AC 85 ms
76,440 KB
testcase_17 AC 90 ms
77,144 KB
testcase_18 AC 116 ms
77,028 KB
testcase_19 AC 99 ms
77,184 KB
testcase_20 AC 87 ms
76,384 KB
testcase_21 AC 85 ms
76,696 KB
testcase_22 AC 97 ms
76,964 KB
testcase_23 AC 72 ms
71,272 KB
testcase_24 AC 74 ms
71,368 KB
testcase_25 AC 73 ms
71,252 KB
testcase_26 AC 93 ms
77,324 KB
testcase_27 AC 85 ms
76,184 KB
testcase_28 AC 86 ms
76,236 KB
testcase_29 AC 88 ms
76,456 KB
testcase_30 AC 121 ms
77,284 KB
testcase_31 AC 143 ms
77,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

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

q = [0]
par = [-1] * N
topo = []
while q:
    s = q.pop()
    topo.append(s)
    for t, d in G[s]:
        if t == par[s]:
            continue
        par[t] = s
        G[t].remove((s, d))
        q.append(t)

cnt = [0]*N
for s, g in enumerate(G):
    if not g:
        cnt[s] = 1

ans = 0
knapsack = []
for s in topo[::-1]:
    p = par[s]
    if p != -1:
        cnt[p] += cnt[s]
    for t, c in G[s]:
        ans += c * cnt[t]
        knapsack.append((c, c * cnt[t]))

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