結果

問題 No.1488 Max Score of the Tree
コンテスト
ユーザー ああいい
提出日時 2021-12-28 21:07:55
言語 PyPy3
(7.3.23 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 167 ms / 2,000 ms
+ 840µs
コード長 920 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 71 ms
コンパイル使用メモリ 80,896 KB
実行使用メモリ 145,664 KB
最終ジャッジ日時 2026-09-09 01:50:36
合計ジャッジ時間 5,001 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

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

node = [[0] * 2 for _ in range(N + 1)]
edge = []
score = 0
def tansaku(now = 1,parent = 0):
    global score
    node[now][0] = parent
    if now != 1 and len(G[now]) == 1:
        node[now][1] = 1
        return 1
    for v,c in G[now]:
        if v != parent:
            t = tansaku(v,now)
            node[now][1] += t
            edge.append((c,t))
            score += c * t
    return node[now][1]
tansaku()
dp = [[0] * (K + 1) for _ in range(N-1)]
dp[0][edge[0][0]] = edge[0][1] * edge[0][0]
for i in range(1,N-1):
    c,t = edge[i]
    for j in range(K + 1):
        dp[i][j] = dp[i-1][j]
        if j - c >= 0:
            if dp[i][j] < dp[i-1][j-c] + c * t:
                dp[i][j] = dp[i-1][j-c] + c * t
m = max(dp[N-2])
print(m + score)
0