結果
問題 |
No.1488 Max Score of the Tree
|
ユーザー |
![]() |
提出日時 | 2021-04-23 23:07:56 |
言語 | PyPy3 (7.3.15) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,214 bytes |
コンパイル時間 | 215 ms |
コンパイル使用メモリ | 82,432 KB |
実行使用メモリ | 151,424 KB |
最終ジャッジ日時 | 2024-07-04 08:41:06 |
合計ジャッジ時間 | 2,867 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge1 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 22 WA * 7 |
ソースコード
import sys input = sys.stdin.readline from operator import itemgetter N,K=map(int,input().split()) E=[tuple(map(int,input().split())) for i in range(N-1)] EDGE=[[] for i in range(N)] for a,b,e in E: a-=1 b-=1 EDGE[a].append((b,e)) EDGE[b].append((a,e)) QUE=[0] Parent=[-1]*(N+1) Parent[0]=0 TOP_SORT=[] while QUE: x=QUE.pop() TOP_SORT.append(x) for to,w in EDGE[x]: if Parent[to]==-1: Parent[to]=x,w QUE.append(to) EW=[0]*N for n in TOP_SORT[1:][::-1]: #print(n,Parent[n]) if EW[n]==0: EW[n]=1 EW[Parent[n][0]]+=EW[n] XX=[] for i in range(1,N): XX.append((Parent[i][1],EW[i])) XC=[[] for i in range(101)] for x,c in XX: XC[c].append(x) ANS=0 cost=0 for i in range(100,-1,-1): if sum(XC[i])+cost<K: ANS+=sum(XC[i])*i*2 cost+=sum(XC[i]) else: rest=K-cost DP={0} for j in XC[i]: NDP=DP.copy() for d in DP: if d+j<=rest: NDP.add(d+j) DP=NDP MAX=max(DP) SUM=sum(XC[i]) ANS+=((SUM-MAX)+MAX*2)*i cost+=MAX print(ANS)