結果

問題 No.3222 Let the World Forget Me
ユーザー sasa8uyauya
提出日時 2025-08-01 22:44:18
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 419 ms / 2,000 ms
コード長 741 bytes
コンパイル時間 192 ms
コンパイル使用メモリ 82,348 KB
実行使用メモリ 101,364 KB
最終ジャッジ日時 2025-08-01 22:44:28
合計ジャッジ時間 10,076 ms
ジャッジサーバーID
(参考情報)
judge3 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 1
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

n,m=map(int,input().split())
p=list(map(int,input().split()))
e=[[] for i in range(n)]
for i in range(n-1):
  u,v=map(int,input().split())
  u-=1
  v-=1
  e[u]+=[v]
  e[v]+=[u]
d=[len(e[i]) for i in range(n)]
v=[0]*n
c=list(map(int,input().split()))
q1=[]
for i in c:
  i-=1
  v[i]=1
  q1+=[i]
from heapq import heappush,heappop
q2=[]
for i in range(n):
  if d[i]==1 and v[i]==0:
    heappush(q2,(-p[i],i))
ans=0
for _ in range(n):
  while len(q2)>0 and v[q2[0][1]]:
    heappop(q2)
  if len(q2)>0:
    a,i=heappop(q2)
    a=-a
    ans+=a
    v[i]=1
    for j in e[i]:
      d[j]-=1
      if d[j]==1:
        heappush(q2,(-p[j],j))
  nq=[]
  for i in q1:
    for j in e[i]:
      if v[j]==0:
        v[j]=1
        nq+=[j]
  q1=nq
print(ans)
0