結果

問題 No.1917 LCMST
ユーザー とりゐとりゐ
提出日時 2023-01-28 19:42:50
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 2,796 ms / 4,000 ms
コード長 1,583 bytes
コンパイル時間 1,048 ms
コンパイル使用メモリ 86,848 KB
実行使用メモリ 440,848 KB
最終ジャッジ日時 2023-09-11 07:41:18
合計ジャッジ時間 61,276 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 115 ms
78,868 KB
testcase_01 AC 116 ms
78,852 KB
testcase_02 AC 115 ms
78,820 KB
testcase_03 AC 216 ms
81,576 KB
testcase_04 AC 210 ms
82,636 KB
testcase_05 AC 209 ms
81,472 KB
testcase_06 AC 215 ms
83,172 KB
testcase_07 AC 206 ms
82,084 KB
testcase_08 AC 116 ms
78,812 KB
testcase_09 AC 2,715 ms
440,848 KB
testcase_10 AC 2,796 ms
375,788 KB
testcase_11 AC 2,461 ms
438,960 KB
testcase_12 AC 2,158 ms
402,848 KB
testcase_13 AC 932 ms
257,612 KB
testcase_14 AC 2,219 ms
342,548 KB
testcase_15 AC 639 ms
238,692 KB
testcase_16 AC 970 ms
261,604 KB
testcase_17 AC 1,245 ms
271,444 KB
testcase_18 AC 1,495 ms
273,828 KB
testcase_19 AC 731 ms
238,620 KB
testcase_20 AC 547 ms
236,560 KB
testcase_21 AC 703 ms
238,668 KB
testcase_22 AC 627 ms
236,640 KB
testcase_23 AC 628 ms
236,132 KB
testcase_24 AC 707 ms
238,740 KB
testcase_25 AC 569 ms
236,792 KB
testcase_26 AC 605 ms
237,596 KB
testcase_27 AC 681 ms
238,440 KB
testcase_28 AC 655 ms
238,664 KB
testcase_29 AC 2,591 ms
378,348 KB
testcase_30 AC 2,588 ms
379,184 KB
testcase_31 AC 2,597 ms
377,392 KB
testcase_32 AC 2,586 ms
377,552 KB
testcase_33 AC 2,559 ms
377,660 KB
testcase_34 AC 298 ms
216,884 KB
testcase_35 AC 300 ms
217,252 KB
testcase_36 AC 299 ms
217,380 KB
testcase_37 AC 2,601 ms
377,476 KB
testcase_38 AC 2,616 ms
378,500 KB
testcase_39 AC 2,544 ms
377,608 KB
testcase_40 AC 2,579 ms
383,644 KB
testcase_41 AC 2,573 ms
380,452 KB
testcase_42 AC 2,496 ms
372,456 KB
testcase_43 AC 384 ms
221,396 KB
testcase_44 AC 370 ms
222,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from collections import defaultdict

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 same(self,x,y):
    return self.find(x)==self.find(y)

def compare(a,b):
  # 比較関数 (a<=b)
  return a[0]<=b[0]


def merge(l, r):
    new = []
    ln = 0
    rn = 0
    while ln < len(l) and rn < len(r):
        if compare(l[ln],r[rn]):
            new.append(l[ln])
            ln += 1
        else:
            new.append(r[rn])
            rn += 1
    for i in range(ln, len(l)):
        new.append(l[i])
    for i in range(rn, len(r)):
        new.append(r[i])
    return new
 
def MergeSort(l):
    if len(l) == 0:
        return []
    if len(l) == 1:
        return l
    else:
        a = l[:len(l)//2]
        b = l[len(l)//2:]
        return merge(MergeSort(a), MergeSort(b))

n=int(input())
a=list(map(int,input().split()))
m=10**5+1
cnt=[0]*m
ans=0
for i in a:
  if cnt[i]==0:
    cnt[i]=1
  else:
    ans+=i

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

edge=MergeSort(edge)
uf=UnionFind(m+1)
for c,x,y in edge:
  if not uf.same(x,y):
    ans+=c
    uf.union(x,y)

print(ans)
0