結果

問題 No.1488 Max Score of the Tree
ユーザー ayaoniayaoni
提出日時 2021-04-23 22:10:16
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 322 ms / 2,000 ms
コード長 1,237 bytes
コンパイル時間 301 ms
コンパイル使用メモリ 87,276 KB
実行使用メモリ 155,452 KB
最終ジャッジ日時 2023-09-17 12:22:59
合計ジャッジ時間 7,874 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 274 ms
152,004 KB
testcase_01 AC 304 ms
148,624 KB
testcase_02 AC 310 ms
152,812 KB
testcase_03 AC 300 ms
155,360 KB
testcase_04 AC 322 ms
155,396 KB
testcase_05 AC 110 ms
72,580 KB
testcase_06 AC 155 ms
96,512 KB
testcase_07 AC 218 ms
121,680 KB
testcase_08 AC 173 ms
108,080 KB
testcase_09 AC 188 ms
99,168 KB
testcase_10 AC 209 ms
123,320 KB
testcase_11 AC 297 ms
155,312 KB
testcase_12 AC 116 ms
77,520 KB
testcase_13 AC 163 ms
88,888 KB
testcase_14 AC 205 ms
114,260 KB
testcase_15 AC 175 ms
100,176 KB
testcase_16 AC 128 ms
81,136 KB
testcase_17 AC 151 ms
91,308 KB
testcase_18 AC 238 ms
129,260 KB
testcase_19 AC 179 ms
105,636 KB
testcase_20 AC 147 ms
88,476 KB
testcase_21 AC 126 ms
82,608 KB
testcase_22 AC 172 ms
99,572 KB
testcase_23 AC 107 ms
72,464 KB
testcase_24 AC 107 ms
72,288 KB
testcase_25 AC 111 ms
72,360 KB
testcase_26 AC 149 ms
94,456 KB
testcase_27 AC 129 ms
77,352 KB
testcase_28 AC 142 ms
85,216 KB
testcase_29 AC 151 ms
88,036 KB
testcase_30 AC 243 ms
140,032 KB
testcase_31 AC 319 ms
155,452 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
from functools import lru_cache
sys.setrecursionlimit(10**7)
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int,sys.stdin.readline().rstrip().split())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def LI2(): return list(map(int,sys.stdin.readline().rstrip()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
def LS2(): return list(sys.stdin.readline().rstrip())


N,K = MI()
Graph = [[] for _ in range(N+1)]
for _ in range(N-1):
    a,b,c = MI()
    Graph[a].append((b,c))
    Graph[b].append((a,c))

X = []
ans = 0


@lru_cache(maxsize=None)
def f(i,par):
    global ans
    count = 0
    for j,c in Graph[i]:
        if j == par:
            continue
        d = f(j,i)
        count += d
        X.append((c,c*d))
        ans += c*d
    if count == 0:
        return 1
    return count


f(1,0)

dp = [[-1]*(K+1) for _ in range(N)]
dp[0][0] = 0
for i in range(1,N):
    w,v = X[i-1]
    for j in range(K+1):
        if dp[i-1][j] >= 0:
            dp[i][j] = dp[i-1][j]
        if j >= w and dp[i-1][j-w] >= 0:
            dp[i][j] = max(dp[i][j],dp[i-1][j-w]+v)

M = max(dp[-1])
if M > 0:
    ans += M

print(ans)
0