結果

問題 No.1917 LCMST
ユーザー とりゐとりゐ
提出日時 2022-02-28 13:43:09
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,780 ms / 4,000 ms
コード長 1,193 bytes
コンパイル時間 564 ms
コンパイル使用メモリ 87,160 KB
実行使用メモリ 321,528 KB
最終ジャッジ日時 2023-09-10 16:55:01
合計ジャッジ時間 40,217 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 83 ms
78,432 KB
testcase_01 AC 84 ms
78,504 KB
testcase_02 AC 83 ms
78,504 KB
testcase_03 AC 111 ms
80,580 KB
testcase_04 AC 109 ms
80,808 KB
testcase_05 AC 109 ms
80,436 KB
testcase_06 AC 109 ms
80,700 KB
testcase_07 AC 107 ms
80,628 KB
testcase_08 AC 82 ms
78,160 KB
testcase_09 AC 1,465 ms
303,492 KB
testcase_10 AC 1,614 ms
304,772 KB
testcase_11 AC 1,438 ms
305,752 KB
testcase_12 AC 1,236 ms
301,548 KB
testcase_13 AC 530 ms
247,580 KB
testcase_14 AC 1,408 ms
302,984 KB
testcase_15 AC 433 ms
246,604 KB
testcase_16 AC 564 ms
247,348 KB
testcase_17 AC 685 ms
246,828 KB
testcase_18 AC 857 ms
248,348 KB
testcase_19 AC 475 ms
247,384 KB
testcase_20 AC 433 ms
248,080 KB
testcase_21 AC 490 ms
247,440 KB
testcase_22 AC 409 ms
249,060 KB
testcase_23 AC 405 ms
248,132 KB
testcase_24 AC 491 ms
247,956 KB
testcase_25 AC 411 ms
247,688 KB
testcase_26 AC 445 ms
249,168 KB
testcase_27 AC 447 ms
247,620 KB
testcase_28 AC 482 ms
249,212 KB
testcase_29 AC 1,716 ms
320,480 KB
testcase_30 AC 1,689 ms
320,124 KB
testcase_31 AC 1,741 ms
320,620 KB
testcase_32 AC 1,776 ms
319,636 KB
testcase_33 AC 1,701 ms
320,940 KB
testcase_34 AC 262 ms
230,324 KB
testcase_35 AC 256 ms
230,372 KB
testcase_36 AC 252 ms
231,124 KB
testcase_37 AC 1,715 ms
319,732 KB
testcase_38 AC 1,780 ms
321,376 KB
testcase_39 AC 1,688 ms
321,528 KB
testcase_40 AC 1,711 ms
320,840 KB
testcase_41 AC 1,675 ms
320,464 KB
testcase_42 AC 1,676 ms
320,036 KB
testcase_43 AC 286 ms
242,332 KB
testcase_44 AC 294 ms
242,880 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * 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

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

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

def MST(n,edge):
  uf=UnionFind(n)
  ans=0
  for cost,u,v in edge:
    if not uf.same(u,v):
      ans+=cost
      uf.union(u,v)
  return ans

n=int(input())
a=list(map(int,input().split()))
m=10**5
d=[-1]*(m+1)
cnt=[0]*(m+1)

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

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

edge.sort(key=lambda x:x[0])
ans+=MST(m+1,edge)
print(ans)
0