結果

問題 No.3222 Let the World Forget Me
ユーザー timi
提出日時 2025-08-03 13:37:27
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 347 ms / 2,000 ms
コード長 802 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 82,476 KB
実行使用メモリ 99,400 KB
最終ジャッジ日時 2025-08-03 13:37:41
合計ジャッジ時間 8,547 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

N,M=map(int, input().split())
A=list(map(int, input().split()))
D=[[] for i in range(N)]
G=[0]*N
from collections import deque
d=deque()
for i in range(N-1):
  u,v=map(int, input().split())
  u-=1;v-=1
  D[u].append(v);D[v].append(u)
  G[u]+=1;G[v]+=1
  
C=list(map(int, input().split()))
P=[0]*N
for c in C:
  d.append(c-1)
  P[c-1]=1
  
import heapq
E=[]
for i in range(N):
  if G[i]==1 and P[i]==0:
    heapq.heappush(E,(-A[i],i))

ans=0
for i in range(N):
  while E:
    x,y=heapq.heappop(E)
    if P[y]==0:
      ans-=x
      for nex in D[y]:
        G[nex]-=1
        if G[nex]==1 and P[nex]==0:
          heapq.heappush(E,(-A[nex],nex))
      break
  nd=deque()
  while d:
    now=d.popleft()
    for nex in D[now]:
      if P[nex]==0:
        P[nex]=1
        nd.append(nex)
  d=nd 
print(ans)
0