結果

問題 No.1917 LCMST
ユーザー chineristACchineristAC
提出日時 2022-03-26 22:49:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,500 ms / 4,000 ms
コード長 2,090 bytes
コンパイル時間 366 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 321,056 KB
最終ジャッジ日時 2023-09-10 16:59:10
合計ジャッジ時間 53,984 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 133 ms
80,800 KB
testcase_01 AC 127 ms
80,896 KB
testcase_02 AC 129 ms
80,484 KB
testcase_03 AC 189 ms
84,164 KB
testcase_04 AC 183 ms
84,280 KB
testcase_05 AC 183 ms
84,024 KB
testcase_06 AC 175 ms
84,200 KB
testcase_07 AC 185 ms
84,536 KB
testcase_08 AC 130 ms
80,988 KB
testcase_09 AC 2,079 ms
321,056 KB
testcase_10 AC 2,142 ms
320,924 KB
testcase_11 AC 2,037 ms
280,984 KB
testcase_12 AC 1,725 ms
304,168 KB
testcase_13 AC 753 ms
252,264 KB
testcase_14 AC 1,908 ms
315,508 KB
testcase_15 AC 586 ms
251,268 KB
testcase_16 AC 783 ms
252,420 KB
testcase_17 AC 1,000 ms
254,172 KB
testcase_18 AC 1,227 ms
258,728 KB
testcase_19 AC 610 ms
251,492 KB
testcase_20 AC 514 ms
251,224 KB
testcase_21 AC 638 ms
251,436 KB
testcase_22 AC 523 ms
251,204 KB
testcase_23 AC 512 ms
251,252 KB
testcase_24 AC 629 ms
251,160 KB
testcase_25 AC 532 ms
251,320 KB
testcase_26 AC 555 ms
251,324 KB
testcase_27 AC 580 ms
251,360 KB
testcase_28 AC 602 ms
251,324 KB
testcase_29 AC 2,427 ms
317,568 KB
testcase_30 AC 2,413 ms
317,676 KB
testcase_31 AC 2,500 ms
317,564 KB
testcase_32 AC 2,440 ms
317,960 KB
testcase_33 AC 2,431 ms
317,864 KB
testcase_34 AC 321 ms
251,904 KB
testcase_35 AC 324 ms
251,820 KB
testcase_36 AC 327 ms
251,900 KB
testcase_37 AC 2,423 ms
317,100 KB
testcase_38 AC 2,384 ms
317,392 KB
testcase_39 AC 2,395 ms
317,072 KB
testcase_40 AC 2,401 ms
317,912 KB
testcase_41 AC 2,386 ms
316,208 KB
testcase_42 AC 2,285 ms
316,708 KB
testcase_43 AC 369 ms
251,092 KB
testcase_44 AC 371 ms
251,860 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(key=lambda e:e[0])    
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