結果

問題 No.3660 LIS on Tree
コンテスト
ユーザー prd_xxx
提出日時 2026-08-30 14:21:52
言語 PyPy3
(7.3.23)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 277 ms / 2,000 ms
+ 465µs
コード長 665 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 809 ms
コンパイル使用メモリ 95,852 KB
実行使用メモリ 130,508 KB
最終ジャッジ日時 2026-08-30 14:22:59
合計ジャッジ時間 5,207 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 20
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import sys
input = sys.stdin.readline
N = int(input())
S = list(map(int,input().split()))
AB = [tuple(map(int,input().split())) for _ in range(N-1)]

G = [[] for _ in range(N)]
indeg = [0]*N
for a,b in AB:
    a,b = a-1,b-1
    if S[a] > S[b]:
        G[a].append(b)
        indeg[b] += 1
    elif S[a] < S[b]:
        G[b].append(a)
        indeg[a] += 1

from collections import deque
q = deque()
score = S[:]
for i in range(N):
    if indeg[i]==0:
        q.append(i)
while q:
    v = q.popleft()
    for to in G[v]:
        indeg[to] -= 1
        score[to] = max(score[to], score[v] + S[to])
        if indeg[to] == 0:
            q.append(to)
print(max(score))
0