結果

問題 No.1917 LCMST
ユーザー tamatotamato
提出日時 2022-04-30 00:26:05
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,606 ms / 4,000 ms
コード長 1,955 bytes
コンパイル時間 1,186 ms
コンパイル使用メモリ 86,256 KB
実行使用メモリ 329,148 KB
最終ジャッジ日時 2023-09-11 16:13:23
合計ジャッジ時間 62,706 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 233 ms
94,584 KB
testcase_01 AC 314 ms
94,440 KB
testcase_02 AC 241 ms
94,520 KB
testcase_03 AC 317 ms
106,720 KB
testcase_04 AC 284 ms
106,600 KB
testcase_05 AC 325 ms
107,428 KB
testcase_06 AC 290 ms
106,804 KB
testcase_07 AC 318 ms
107,516 KB
testcase_08 AC 251 ms
94,404 KB
testcase_09 AC 2,320 ms
327,208 KB
testcase_10 AC 2,271 ms
327,900 KB
testcase_11 AC 2,248 ms
327,604 KB
testcase_12 AC 1,939 ms
310,304 KB
testcase_13 AC 979 ms
238,000 KB
testcase_14 AC 2,061 ms
315,996 KB
testcase_15 AC 745 ms
233,216 KB
testcase_16 AC 955 ms
238,036 KB
testcase_17 AC 1,172 ms
248,516 KB
testcase_18 AC 1,552 ms
278,116 KB
testcase_19 AC 808 ms
232,420 KB
testcase_20 AC 723 ms
229,976 KB
testcase_21 AC 856 ms
232,628 KB
testcase_22 AC 729 ms
230,288 KB
testcase_23 AC 748 ms
230,092 KB
testcase_24 AC 853 ms
233,056 KB
testcase_25 AC 719 ms
230,144 KB
testcase_26 AC 753 ms
230,824 KB
testcase_27 AC 760 ms
231,200 KB
testcase_28 AC 800 ms
231,728 KB
testcase_29 AC 2,565 ms
328,912 KB
testcase_30 AC 2,534 ms
328,700 KB
testcase_31 AC 2,586 ms
328,656 KB
testcase_32 AC 2,479 ms
328,716 KB
testcase_33 AC 2,541 ms
328,872 KB
testcase_34 AC 501 ms
226,724 KB
testcase_35 AC 488 ms
226,888 KB
testcase_36 AC 490 ms
226,768 KB
testcase_37 AC 2,489 ms
328,924 KB
testcase_38 AC 2,467 ms
328,836 KB
testcase_39 AC 2,491 ms
329,148 KB
testcase_40 AC 2,606 ms
328,776 KB
testcase_41 AC 2,598 ms
328,872 KB
testcase_42 AC 2,501 ms
328,868 KB
testcase_43 AC 534 ms
229,876 KB
testcase_44 AC 537 ms
230,208 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

mod = 998244353
NN = 10 ** 5


def main():
    import sys
    from collections import Counter
    input = sys.stdin.readline

    class UnionFind():
        def __init__(self, n):
            self.n = n
            self.root = [-1] * (n + 1)
            self.rnk = [0] * (n + 1)

        def find_root(self, x):
            while self.root[x] >= 0:
                x = self.root[x]
            return x

        def unite(self, x, y):
            x = self.find_root(x)
            y = self.find_root(y)
            if x == y:
                return
            elif self.rnk[x] > self.rnk[y]:
                self.root[x] += self.root[y]
                self.root[y] = x
            else:
                self.root[y] += self.root[x]
                self.root[x] = y
                if self.rnk[x] == self.rnk[y]:
                    self.rnk[y] += 1

        def isSameGroup(self, x, y):
            return self.find_root(x) == self.find_root(y)

        def size(self, x):
            return -self.root[self.find_root(x)]

    div = [[] for _ in range(NN + 1)]
    for d in range(1, NN + 1):
        for x in range(1, NN + 1):
            if d * x > NN:
                break
            div[d * x].append(d)

    N = int(input())
    A = list(map(int, input().split()))

    C = Counter(A)
    ans = 0
    A_sorted = sorted(list(set(A)))
    D = [[] for _ in range(NN + 1)]
    for i, a in enumerate(A_sorted):
        ans += a * (C[a] - 1)
        for d in div[a]:
            D[d].append(a)

    if len(A_sorted) == 1:
        print(ans)
        exit()

    UF = UnionFind(NN)
    E = []
    for d in range(1, NN + 1):
        if len(D[d]) > 1:
            a0 = D[d][0]
            for a in D[d][1:]:
                E.append((a * a0 // d, a, a0))
    E.sort(key=lambda x: x[0])
    for x, a, b in E:
        if UF.isSameGroup(a, b):
            continue
        ans += x
        UF.unite(a, b)
    print(ans)


if __name__ == '__main__':
    main()
0