結果

問題 No.417 チューリップバブル
ユーザー titiatitia
提出日時 2023-03-16 02:05:34
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 67 ms / 2,000 ms
コード長 1,197 bytes
コンパイル時間 170 ms
コンパイル使用メモリ 81,828 KB
実行使用メモリ 70,400 KB
最終ジャッジ日時 2023-10-18 12:46:57
合計ジャッジ時間 3,969 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
55,660 KB
testcase_01 AC 42 ms
55,660 KB
testcase_02 AC 41 ms
55,660 KB
testcase_03 AC 41 ms
55,660 KB
testcase_04 AC 40 ms
55,660 KB
testcase_05 AC 41 ms
55,660 KB
testcase_06 AC 42 ms
55,660 KB
testcase_07 AC 42 ms
55,660 KB
testcase_08 AC 49 ms
63,924 KB
testcase_09 AC 54 ms
66,228 KB
testcase_10 AC 51 ms
63,924 KB
testcase_11 AC 54 ms
66,216 KB
testcase_12 AC 56 ms
66,228 KB
testcase_13 AC 51 ms
63,924 KB
testcase_14 AC 56 ms
66,216 KB
testcase_15 AC 52 ms
63,924 KB
testcase_16 AC 51 ms
63,924 KB
testcase_17 AC 53 ms
63,924 KB
testcase_18 AC 53 ms
63,924 KB
testcase_19 AC 53 ms
63,924 KB
testcase_20 AC 58 ms
66,284 KB
testcase_21 AC 59 ms
66,284 KB
testcase_22 AC 62 ms
68,352 KB
testcase_23 AC 59 ms
66,216 KB
testcase_24 AC 43 ms
55,660 KB
testcase_25 AC 59 ms
66,216 KB
testcase_26 AC 52 ms
63,924 KB
testcase_27 AC 56 ms
66,216 KB
testcase_28 AC 61 ms
68,284 KB
testcase_29 AC 59 ms
66,216 KB
testcase_30 AC 61 ms
68,276 KB
testcase_31 AC 59 ms
66,284 KB
testcase_32 AC 45 ms
61,132 KB
testcase_33 AC 52 ms
64,168 KB
testcase_34 AC 54 ms
66,216 KB
testcase_35 AC 66 ms
70,380 KB
testcase_36 AC 65 ms
70,380 KB
testcase_37 AC 66 ms
70,380 KB
testcase_38 AC 66 ms
70,380 KB
testcase_39 AC 67 ms
70,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N,M=map(int,input().split())
U=[int(input()) for i in range(N)]

E=[[] for i in range(N)]
COST=dict()
for i in range(N-1):
    a,b,c=map(int,input().split())
    E[a].append((b,c))
    E[b].append((a,c))

    COST[a,b]=c
    COST[b,a]=c

# オイラーツアー
from collections import deque
QUE = deque([0])
QUE2 = deque()
EULER=[] # これが1からツアーで辿った点
USED=[0]*(N+1)
while QUE:
    x=QUE.pop()
    EULER.append(x)
    if USED[x]==1:
        continue
    for to,_ in E[x]:

        if USED[to]==0:
            QUE2.append(to)
        else:
            QUE.append(to)
    QUE.extend(QUE2)
    QUE2=deque()

    USED[x]=1

DP=[[0]*(M+1) for i in range(N)]
DP[0][0]=U[0]

USE=[0]*N
FIRST=[0]*len(EULER)

for i in range(len(EULER)):
    if USE[EULER[i]]==0:
        FIRST[i]=1
        USE[EULER[i]]=1

for i in range(len(EULER)-1):
    x,y=EULER[i],EULER[i+1]
    cost=COST[x,y]

    if FIRST[i+1]==1:
        for j in range(M-cost,-1,-1):
            DP[y][j+cost]=max(DP[y][j+cost],DP[x][j]+U[y])

    else:
        for j in range(M-cost,-1,-1):
            DP[y][j+cost]=max(DP[y][j+cost],DP[x][j])

print(max(DP[0]))
        
        
0