結果

問題 No.1917 LCMST
ユーザー とりゐ
提出日時 2025-05-11 03:52:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,869 ms / 4,000 ms
コード長 1,268 bytes
コンパイル時間 585 ms
コンパイル使用メモリ 82,656 KB
実行使用メモリ 319,876 KB
最終ジャッジ日時 2025-05-11 03:53:25
合計ジャッジ時間 43,012 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind:
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n
        self.group_count = n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x
        self.group_count -= 1

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)


N = int(input())
A = list(map(int, input().split()))
M = max(A) + 1
cnt = [0] * M

ans = 0
for i in A:
    if cnt[i]:
        ans += i
    else:
        cnt[i] = 1

edge = []
for i in range(1, M + 1):
    res = -1
    for j in range(i, M, i):
        if cnt[j]:
            if res == -1:
                res = j
            else:
                edge.append((res * j // i, res, j))

edge.sort(key=lambda x: x[0])

uf = UnionFind(M)
for w, i, j in edge:
    if not uf.same(i, j):
        ans += w
        uf.union(i, j)

print(ans)
0