結果

問題 No.3222 Let the World Forget Me
ユーザー kidodesu
提出日時 2025-08-01 21:54:51
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 524 ms / 2,000 ms
コード長 1,093 bytes
コンパイル時間 201 ms
コンパイル使用メモリ 82,648 KB
実行使用メモリ 100,996 KB
最終ジャッジ日時 2025-08-01 21:55:05
合計ジャッジ時間 12,178 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

n, m = map(int, input().split())
P = list(map(int, input().split()))
node = [[] for _ in range(n)]
for _ in range(n-1):
    u, v = [int(x)-1 for x in input().split()]
    node[u].append(v)
    node[v].append(u)
C = list(map(int, input().split()))
D = [-1] * n
for i in range(len(C)):
    C[i] -= 1
    D[C[i]] = 0

from collections import deque
dq = deque(C)
while dq:
    now = dq.popleft()
    for nxt in node[now]:
        if D[nxt] == -1:
            D[nxt] = D[now] + 1
            dq.append(nxt)

I = [i for i in range(n)]
I.sort(key = lambda x: -P[x])
ans = 0
cnt = 0
V = [0] * n
for now in range(n):
    for nxt in node[now]:
        if now < nxt:
            V[now] += 1
            V[nxt] += 1

from heapq import *
hq = []
for now in range(n):
    if V[now] == 1:
        heappush(hq, -P[now] * n + now)

while hq:
    mask = heappop(hq)
    now = mask % n
    if D[now] > cnt:
        ans += P[now]
        cnt += 1
        for nxt in node[now]:
            V[nxt] -= 1
            V[now] -= 1
            if V[nxt] == 1:
                heappush(hq, -P[nxt] * n + nxt)

print(ans)
0