結果
| 問題 | No.860 買い物 |
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2026-05-29 21:56:05 |
| 言語 | PyPy3 (7.3.17) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 454 bytes |
| 記録 | |
| コンパイル時間 | 418 ms |
| コンパイル使用メモリ | 84,992 KB |
| 実行使用メモリ | 114,352 KB |
| 最終ジャッジ日時 | 2026-05-29 21:56:19 |
| 合計ジャッジ時間 | 13,298 ms |
|
ジャッジサーバーID (参考情報) |
judge1_1 / judge2_0 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 3 |
| other | AC * 13 TLE * 2 |
ソースコード
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)