結果

問題 No.1917 LCMST
コンテスト
ユーザー ああ
提出日時 2026-05-24 18:23:50
言語 PyPy3
(7.3.17)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
WA  
実行時間 -
コード長 1,038 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 322 ms
コンパイル使用メモリ 85,292 KB
実行使用メモリ 227,760 KB
最終ジャッジ日時 2026-05-24 18:24:33
合計ジャッジ時間 27,468 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 9 WA * 33
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

import heapq

def aa(m):
    c=[]
    while m!=uf[m]:
        c.append(m)
        m=uf[m]
    for i in c:
        uf[i]=m
    return m

n=int(input())
a=list(map(int,input().split()))
if min(a)==1:
    ans=-1
    for i in a:
        ans+=i
    print(ans)
    exit()
x=[0]*(10**5+1)
for i in a:
    x[i]+=1
v=[];ans=0
c=[[] for i in range(len(x))]
d=[1<<60]*len(x)
f=[0]*len(x)
uf=[i for i in range(len(x))]
for i in range(len(x)):
    if x[i]:
        ans+=(x[i]-1)*i
        for j in range(1,int(i**0.5)+1):
            if i%j==0:
                heapq.heappush(c[j],(i//j,i))
                if j!=i//j:
                    heapq.heappush(c[i//j],(j,i))
for i in range(len(x)):
    if len(c[i])>=2:
        q,w=heapq.heappop(c[i])
        d[i]=q*i;f[i]=w
        q,w=heapq.heappop(c[i])
        heapq.heappush(v,(d[i]*q,i,w))

while v:
    q,w,e=heapq.heappop(v)
    if aa(e)==aa(f[w]):
        continue
    ans+=q
    uf[aa(e)]=aa(f[w])
    if c[w]:
        a,b=heapq.heappop(c[w])
        heapq.heappush(v,(d[w]*a,w,b))

print(ans)


0