結果

問題 No.1917 LCMST
ユーザー aaaaaaaaaa2230
提出日時 2022-04-30 10:28:52
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,775 ms / 4,000 ms
コード長 1,124 bytes
コンパイル時間 694 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 275,908 KB
最終ジャッジ日時 2024-06-29 16:07:58
合計ジャッジ時間 57,276 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

class Unionfind:
     
    def __init__(self,n):
        self.uf = [-1]*n
 
    def find(self,x):
        if self.uf[x] < 0:
            return x
        else:
            self.uf[x] = self.find(self.uf[x])
            return self.uf[x]
 
    def same(self,x,y):
        return self.find(x) == self.find(y)
 
    def union(self,x,y):
        x = self.find(x)
        y = self.find(y)
        if x == y:
            return False
        if self.uf[x] > self.uf[y]:
            x,y = y,x
        self.uf[x] += self.uf[y]
        self.uf[y] = x
        return True
 
    def size(self,x):
        x = self.find(x)
        return -self.uf[x]


n = int(input())
A = list(map(int,input().split()))
M = 10**5+5
ans = 0
count = [0]*M
for a in A:
    if count[a]:
        ans += a
    else:
        count[a] = 1


edge = []
uf = Unionfind(M)
for i in range(1,M):
    find = 0
    for j in range(i,M,i):
        if count[j] == 0:
            continue
        if find:
            edge.append([j*find//i,find,j])
        else:
            find = j

edge.sort()
for cost,x,y in edge:
    if uf.union(x,y):
        ans += cost
print(ans)
0