結果

問題 No.1917 LCMST
ユーザー chineristACchineristAC
提出日時 2022-04-05 00:07:56
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,133 ms / 4,000 ms
コード長 2,073 bytes
コンパイル時間 339 ms
コンパイル使用メモリ 87,104 KB
実行使用メモリ 311,208 KB
最終ジャッジ日時 2023-09-10 17:02:01
合計ジャッジ時間 63,962 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 126 ms
81,052 KB
testcase_01 AC 129 ms
81,008 KB
testcase_02 AC 128 ms
80,816 KB
testcase_03 AC 189 ms
84,160 KB
testcase_04 AC 185 ms
84,116 KB
testcase_05 AC 182 ms
83,956 KB
testcase_06 AC 178 ms
84,136 KB
testcase_07 AC 182 ms
83,908 KB
testcase_08 AC 128 ms
81,036 KB
testcase_09 AC 2,674 ms
311,208 KB
testcase_10 AC 2,683 ms
310,880 KB
testcase_11 AC 2,469 ms
280,836 KB
testcase_12 AC 2,112 ms
281,880 KB
testcase_13 AC 864 ms
252,344 KB
testcase_14 AC 2,260 ms
293,372 KB
testcase_15 AC 613 ms
251,560 KB
testcase_16 AC 933 ms
252,688 KB
testcase_17 AC 1,181 ms
254,096 KB
testcase_18 AC 1,453 ms
254,612 KB
testcase_19 AC 701 ms
251,012 KB
testcase_20 AC 553 ms
251,304 KB
testcase_21 AC 730 ms
251,532 KB
testcase_22 AC 564 ms
251,228 KB
testcase_23 AC 552 ms
250,956 KB
testcase_24 AC 722 ms
251,488 KB
testcase_25 AC 568 ms
251,296 KB
testcase_26 AC 605 ms
251,380 KB
testcase_27 AC 650 ms
251,372 KB
testcase_28 AC 681 ms
251,352 KB
testcase_29 AC 3,051 ms
305,532 KB
testcase_30 AC 3,063 ms
305,124 KB
testcase_31 AC 3,070 ms
305,464 KB
testcase_32 AC 3,066 ms
305,060 KB
testcase_33 AC 3,133 ms
305,464 KB
testcase_34 AC 321 ms
251,680 KB
testcase_35 AC 320 ms
251,576 KB
testcase_36 AC 321 ms
252,052 KB
testcase_37 AC 3,020 ms
305,088 KB
testcase_38 AC 3,057 ms
305,448 KB
testcase_39 AC 3,018 ms
304,768 KB
testcase_40 AC 3,010 ms
305,456 KB
testcase_41 AC 3,013 ms
304,092 KB
testcase_42 AC 2,872 ms
304,368 KB
testcase_43 AC 359 ms
251,104 KB
testcase_44 AC 381 ms
252,040 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFindVerSize():
    def __init__(self, N):
        self._parent = [n for n in range(0, N)]
        self._size = [1] * N
        self.group = N

    def find_root(self, x):
        if self._parent[x] == x: return x
        self._parent[x] = self.find_root(self._parent[x])
        stack = [x]
        while self._parent[stack[-1]]!=stack[-1]:
            stack.append(self._parent[stack[-1]])
        for v in stack:
            self._parent[v] = stack[-1]
        return self._parent[x]

    def unite(self, x, y):
        gx = self.find_root(x)
        gy = self.find_root(y)
        if gx == gy: return

        self.group -= 1

        if self._size[gx] < self._size[gy]:
            self._parent[gx] = gy
            self._size[gy] += self._size[gx]
        else:
            self._parent[gy] = gx
            self._size[gx] += self._size[gy]

    def get_size(self, x):
        return self._size[self.find_root(x)]

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

import sys,random,bisect
from collections import deque,defaultdict
from heapq import heapify,heappop,heappush
from itertools import permutations
from math import log,gcd

input = lambda :sys.stdin.readline().rstrip()
mi = lambda :map(int,input().split())
li = lambda :list(mi())

M = 10**5

N = int(input())
A = li()

res = 0
cnt = [0] * (M+1)
for a in A:
    cnt[a] += 1

val_to_idx = [-1] * (M+1)
B = []
for i in range(1,M+1):
    if cnt[i]:
        val_to_idx[i] = len(B)
        B.append(i)
        res += (cnt[i]-1) * i
        cnt[i] = 1

n = len(B)
edge = []
for val in range(1,M+1):
    mini = M+1
    for i in range(val,M+1,val):
        if cnt[i]:
            mini = i
            break
    if mini == M+1:
        continue
    for i in range(val,M+1,val):
        if cnt[i] and i!=mini:
            u = val_to_idx[mini]
            v = val_to_idx[i]
            edge.append((mini*i//val,u,v))

edge.sort()    
uf = UnionFindVerSize(n)
for c,u,v in edge:
    if not uf.is_same_group(u,v):
        uf.unite(u,v)
        res += c

print(res)


0