結果

問題 No.1488 Max Score of the Tree
ユーザー titiatitia
提出日時 2021-04-23 23:07:56
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,214 bytes
コンパイル時間 268 ms
コンパイル使用メモリ 86,564 KB
実行使用メモリ 161,224 KB
最終ジャッジ日時 2023-09-17 13:03:07
合計ジャッジ時間 4,407 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 AC 78 ms
71,140 KB
testcase_04 AC 79 ms
71,304 KB
testcase_05 AC 77 ms
71,192 KB
testcase_06 AC 85 ms
75,720 KB
testcase_07 AC 121 ms
102,260 KB
testcase_08 AC 81 ms
75,996 KB
testcase_09 AC 119 ms
94,996 KB
testcase_10 AC 92 ms
81,068 KB
testcase_11 AC 207 ms
161,224 KB
testcase_12 AC 78 ms
71,300 KB
testcase_13 WA -
testcase_14 WA -
testcase_15 AC 78 ms
71,164 KB
testcase_16 WA -
testcase_17 AC 79 ms
70,936 KB
testcase_18 AC 80 ms
71,320 KB
testcase_19 WA -
testcase_20 AC 79 ms
71,152 KB
testcase_21 AC 79 ms
71,140 KB
testcase_22 AC 80 ms
71,128 KB
testcase_23 AC 79 ms
70,988 KB
testcase_24 AC 78 ms
71,204 KB
testcase_25 AC 79 ms
70,812 KB
testcase_26 AC 80 ms
71,052 KB
testcase_27 AC 84 ms
75,656 KB
testcase_28 AC 86 ms
76,108 KB
testcase_29 AC 84 ms
75,508 KB
testcase_30 AC 82 ms
75,816 KB
testcase_31 AC 135 ms
103,392 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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)
        
            
            
            
0