結果
| 問題 | No.860 買い物 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-29 21:58:07 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
AC
|
| 実行時間 | 934 ms / 1,000 ms |
| コード長 | 454 bytes |
| 記録 | |
| コンパイル時間 | 127 ms |
| コンパイル使用メモリ | 85,160 KB |
| 実行使用メモリ | 114,172 KB |
| 最終ジャッジ日時 | 2026-05-29 21:58:21 |
| 合計ジャッジ時間 | 10,700 ms |
|
ジャッジサーバーID (参考情報) |
judge4_1 / judge3_1 |
| 純コード判定待ち |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 15 |
ソースコード
import heapq
def aa(m):
c=[]
while m!=uf[m]:
c.append(m)
m=uf[m]
for i in c:
uf[i]=m
return m
n=int(input())
ans=0
uf=[i for i in range(n+1)];hq=[]
for i in range(1,n+1):
a,b=map(int,input().split())
ans+=a
heapq.heappush(hq,[a,0,i])
if i-1:
heapq.heappush(hq,[b,i,i-1])
while hq:
q,w,e=heapq.heappop(hq)
a,b=aa(w),aa(e)
if a==b:
continue
ans+=q;uf[a]=b
print(ans)