結果
| 問題 | No.1767 BLUE to RED | 
| コンテスト | |
| ユーザー |  wgrape | 
| 提出日時 | 2024-10-18 16:51:47 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 1,759 ms / 2,000 ms | 
| コード長 | 766 bytes | 
| コンパイル時間 | 455 ms | 
| コンパイル使用メモリ | 82,560 KB | 
| 実行使用メモリ | 316,264 KB | 
| 最終ジャッジ日時 | 2024-10-18 16:52:18 | 
| 合計ジャッジ時間 | 27,297 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 21 | 
ソースコード
N,M = map(int,input().split())
A = list(map(int,input().split()))
B = list(map(int,input().split()))
A = [[A[i], True] for i in range(N)]
B = [[B[i], False] for i in range(M)]
AB = sorted(A + B, key = lambda x:x[0])
G = [[] for i in range(N + M)]
import heapq as hq
q = []
for i in range(N + M - 1):
    G[i].append([i + 1, AB[i + 1][0] - AB[i][0]])
    G[i + 1].append([i, AB[i + 1][0] - AB[i][0]])
    if AB[i][1]:
        hq.heappush(q, (0, i))
if AB[N + M - 1][1]:
    hq.heappush(q, (0, N + M - 1))
    
ans = 0
seen = set()
while q:
    d, v = hq.heappop(q)
    if v in seen:
        continue
    seen.add(v)
    ans += d
    for child, cost in G[v]:
        if child in seen:
            continue
        hq.heappush(q, (cost, child))
        
print(ans)
            
            
            
        