結果

問題 No.1917 LCMST
ユーザー wolgnikwolgnik
提出日時 2022-04-29 22:56:07
言語 PyPy3
(7.3.15)
結果
WA  
実行時間 -
コード長 1,337 bytes
コンパイル時間 259 ms
コンパイル使用メモリ 86,972 KB
実行使用メモリ 238,404 KB
最終ジャッジ日時 2023-09-11 14:22:04
合計ジャッジ時間 18,908 ms
ジャッジサーバーID
(参考情報)
judge12 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 64 ms
71,128 KB
testcase_01 AC 65 ms
71,440 KB
testcase_02 AC 75 ms
76,360 KB
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 AC 66 ms
71,296 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 AC 272 ms
236,304 KB
testcase_35 AC 273 ms
235,964 KB
testcase_36 AC 279 ms
236,368 KB
testcase_37 WA -
testcase_38 WA -
testcase_39 WA -
testcase_40 WA -
testcase_41 WA -
testcase_42 WA -
testcase_43 WA -
testcase_44 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))

class UnionFind():
  def __init__(self, n):
    self.n = n
    self.root = [-1] * (n + 1)
    self.rnk = [0] * (n + 1)
  def Find_Root(self, x):
    if self.root[x] < 0:
      return x
    else:
      self.root[x] = self.Find_Root(self.root[x])
      return self.root[x]
  def Unite(self, x, y):
    x = self.Find_Root(x)
    y = self.Find_Root(y)
    if x == y:
      return 
    elif self.rnk[x] > self.rnk[y]:
      self.root[x] += self.root[y]
      self.root[y] = x
    else:
      self.root[y] += self.root[x]
      self.root[x] = y
      if self.rnk[x] == self.rnk[y]:
        self.rnk[y] += 1
  def SameQuery(self, x, y): return self.Find_Root(x) == self.Find_Root(y)
  def Count(self, x): return -self.root[self.Find_Root(x)]

mx = max(a)
uf = UnionFind(mx)


table = [-1] * (mx + 1)
for i in range(N): table[a[i]] = i
res = 0

for i in range(mx, 0, -1):
  c = (table[i] != -1)
  mn = mx
  t = 0
  if table[i] != -1:
    mn = i
    t = i
  for j in range(i + i, mx + 1, i):
    if uf.SameQuery(i, j) or table[j] == -1: continue
    uf.Unite(i, j)
    c += 1
    t += j
    mn = min(mn, j)
    #print(res, i, j)
  if c > 1: res += (t - mn) * mn // i
  #print(res, t, mn)

for i in range(N):
  if table[a[i]] != i: res += a[i]

print(res)
0