結果

問題 No.1917 LCMST
ユーザー とりゐとりゐ
提出日時 2022-02-28 14:33:11
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,965 ms / 4,000 ms
コード長 1,194 bytes
コンパイル時間 579 ms
コンパイル使用メモリ 86,700 KB
実行使用メモリ 320,732 KB
最終ジャッジ日時 2023-09-10 16:55:59
合計ジャッジ時間 43,515 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
71,172 KB
testcase_01 AC 75 ms
71,320 KB
testcase_02 AC 77 ms
76,056 KB
testcase_03 AC 113 ms
80,356 KB
testcase_04 AC 114 ms
80,588 KB
testcase_05 AC 115 ms
80,364 KB
testcase_06 AC 116 ms
80,324 KB
testcase_07 AC 111 ms
80,236 KB
testcase_08 AC 72 ms
71,148 KB
testcase_09 AC 1,676 ms
304,500 KB
testcase_10 AC 1,704 ms
304,332 KB
testcase_11 AC 1,608 ms
303,532 KB
testcase_12 AC 1,329 ms
288,592 KB
testcase_13 AC 571 ms
247,032 KB
testcase_14 AC 1,462 ms
302,944 KB
testcase_15 AC 459 ms
246,712 KB
testcase_16 AC 607 ms
247,692 KB
testcase_17 AC 735 ms
247,124 KB
testcase_18 AC 914 ms
246,992 KB
testcase_19 AC 508 ms
248,740 KB
testcase_20 AC 434 ms
248,320 KB
testcase_21 AC 533 ms
247,184 KB
testcase_22 AC 439 ms
248,072 KB
testcase_23 AC 428 ms
248,864 KB
testcase_24 AC 521 ms
247,292 KB
testcase_25 AC 437 ms
248,820 KB
testcase_26 AC 477 ms
248,296 KB
testcase_27 AC 478 ms
248,400 KB
testcase_28 AC 497 ms
248,072 KB
testcase_29 AC 1,947 ms
319,976 KB
testcase_30 AC 1,953 ms
319,824 KB
testcase_31 AC 1,932 ms
320,732 KB
testcase_32 AC 1,944 ms
319,308 KB
testcase_33 AC 1,946 ms
319,620 KB
testcase_34 AC 270 ms
230,032 KB
testcase_35 AC 262 ms
229,536 KB
testcase_36 AC 268 ms
230,456 KB
testcase_37 AC 1,926 ms
319,548 KB
testcase_38 AC 1,965 ms
320,232 KB
testcase_39 AC 1,933 ms
319,500 KB
testcase_40 AC 1,936 ms
319,960 KB
testcase_41 AC 1,925 ms
320,124 KB
testcase_42 AC 1,840 ms
320,076 KB
testcase_43 AC 299 ms
242,748 KB
testcase_44 AC 309 ms
243,488 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=max(a)
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