結果

問題 No.1488 Max Score of the Tree
ユーザー 👑 rin204rin204
提出日時 2022-01-27 20:53:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 123 ms / 2,000 ms
コード長 731 bytes
コンパイル時間 510 ms
コンパイル使用メモリ 86,968 KB
実行使用メモリ 76,876 KB
最終ジャッジ日時 2023-08-26 22:44:35
合計ジャッジ時間 5,321 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 110 ms
76,640 KB
testcase_01 AC 113 ms
76,808 KB
testcase_02 AC 103 ms
76,876 KB
testcase_03 AC 116 ms
76,644 KB
testcase_04 AC 118 ms
76,772 KB
testcase_05 AC 72 ms
71,200 KB
testcase_06 AC 88 ms
76,512 KB
testcase_07 AC 101 ms
76,420 KB
testcase_08 AC 85 ms
76,488 KB
testcase_09 AC 89 ms
76,016 KB
testcase_10 AC 96 ms
76,736 KB
testcase_11 AC 116 ms
76,824 KB
testcase_12 AC 73 ms
76,160 KB
testcase_13 AC 83 ms
76,136 KB
testcase_14 AC 95 ms
76,640 KB
testcase_15 AC 87 ms
76,288 KB
testcase_16 AC 79 ms
76,192 KB
testcase_17 AC 85 ms
76,432 KB
testcase_18 AC 100 ms
76,664 KB
testcase_19 AC 91 ms
76,480 KB
testcase_20 AC 83 ms
76,328 KB
testcase_21 AC 82 ms
76,536 KB
testcase_22 AC 88 ms
76,380 KB
testcase_23 AC 72 ms
71,080 KB
testcase_24 AC 72 ms
71,472 KB
testcase_25 AC 72 ms
71,292 KB
testcase_26 AC 82 ms
76,644 KB
testcase_27 AC 78 ms
75,948 KB
testcase_28 AC 81 ms
76,012 KB
testcase_29 AC 83 ms
76,192 KB
testcase_30 AC 108 ms
76,868 KB
testcase_31 AC 123 ms
76,784 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

def dfs(pos, bpos, cc):
    if len(edges[pos]) == 1:
        ret = 1
        if pos != 0:
            global ans
            ans += cc
        
    else:
        ret = 0
    for npos, c in edges[pos]:
        if npos == bpos:
            continue
        tmp = dfs(npos, pos, cc + c)
        wv.append((c, c * tmp))
        ret += tmp
    return ret
    
dfs(0, -1, 0)

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



0