結果

問題 No.1917 LCMST
ユーザー とりゐとりゐ
提出日時 2023-01-28 19:26:20
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,877 ms / 4,000 ms
コード長 953 bytes
コンパイル時間 1,998 ms
コンパイル使用メモリ 86,996 KB
実行使用メモリ 312,408 KB
最終ジャッジ日時 2023-09-11 07:33:24
合計ジャッジ時間 48,152 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 121 ms
79,112 KB
testcase_01 AC 120 ms
79,120 KB
testcase_02 AC 119 ms
78,716 KB
testcase_03 AC 144 ms
79,324 KB
testcase_04 AC 151 ms
79,312 KB
testcase_05 AC 139 ms
79,488 KB
testcase_06 AC 132 ms
79,204 KB
testcase_07 AC 125 ms
79,324 KB
testcase_08 AC 109 ms
78,932 KB
testcase_09 AC 1,580 ms
309,596 KB
testcase_10 AC 1,625 ms
310,092 KB
testcase_11 AC 1,511 ms
309,008 KB
testcase_12 AC 1,243 ms
299,512 KB
testcase_13 AC 561 ms
239,328 KB
testcase_14 AC 1,371 ms
312,408 KB
testcase_15 AC 460 ms
238,924 KB
testcase_16 AC 581 ms
239,544 KB
testcase_17 AC 715 ms
239,484 KB
testcase_18 AC 891 ms
254,096 KB
testcase_19 AC 514 ms
239,316 KB
testcase_20 AC 442 ms
236,348 KB
testcase_21 AC 533 ms
239,340 KB
testcase_22 AC 444 ms
236,716 KB
testcase_23 AC 442 ms
236,452 KB
testcase_24 AC 536 ms
239,092 KB
testcase_25 AC 448 ms
236,880 KB
testcase_26 AC 472 ms
237,484 KB
testcase_27 AC 502 ms
239,024 KB
testcase_28 AC 505 ms
239,088 KB
testcase_29 AC 1,818 ms
312,252 KB
testcase_30 AC 1,844 ms
312,020 KB
testcase_31 AC 1,813 ms
312,060 KB
testcase_32 AC 1,808 ms
311,876 KB
testcase_33 AC 1,877 ms
312,256 KB
testcase_34 AC 287 ms
217,152 KB
testcase_35 AC 286 ms
217,292 KB
testcase_36 AC 290 ms
217,656 KB
testcase_37 AC 1,804 ms
312,096 KB
testcase_38 AC 1,743 ms
312,148 KB
testcase_39 AC 1,726 ms
312,196 KB
testcase_40 AC 1,765 ms
311,916 KB
testcase_41 AC 1,752 ms
312,036 KB
testcase_42 AC 1,685 ms
312,040 KB
testcase_43 AC 314 ms
221,948 KB
testcase_44 AC 314 ms
222,952 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)

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.sort(key=lambda x:x[0])
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