結果
| 問題 |
No.1488 Max Score of the Tree
|
| コンテスト | |
| ユーザー |
ayaoni
|
| 提出日時 | 2021-04-23 22:10:16 |
| 言語 | PyPy3 (7.3.15) |
| 結果 |
AC
|
| 実行時間 | 255 ms / 2,000 ms |
| コード長 | 1,237 bytes |
| コンパイル時間 | 206 ms |
| コンパイル使用メモリ | 82,432 KB |
| 実行使用メモリ | 147,200 KB |
| 最終ジャッジ日時 | 2024-07-04 08:09:21 |
| 合計ジャッジ時間 | 5,915 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge1 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 29 |
ソースコード
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)
ayaoni