#include namespace { #pragma GCC diagnostic ignored "-Wunused-function" #include #pragma GCC diagnostic warning "-Wunused-function" using namespace std; using namespace atcoder; #define rep(i,n) for(int i = 0; i < (int)(n); i++) #define rrep(i,n) for(int i = (int)(n) - 1; i >= 0; i--) #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) template bool chmax(T& a, const T& b) { if (a < b) { a = b; return true; } else return false; } template bool chmin(T& a, const T& b) { if (b < a) { a = b; return true; } else return false; } using ll = long long; using P = pair; using VI = vector; using VVI = vector; using VL = vector; using VVL = vector; pair, vector> primes_lpf(const int n) { vector primes; primes.reserve(n / 10); vector lpf(n + 1); for (int i = 2; i <= n; i += 2) lpf[i] = 2; for (int i = 3; i <= n; i += 6) lpf[i] = 3; if (2 <= n) primes.push_back(2); if (3 <= n) primes.push_back(3); // 5 * x <= n, x <= floor(n / 5) const int n5 = n / 5; int x = 5; char add_next = 2; for (; x <= n5; x += add_next, add_next ^= 0x2 ^ 4) { int px = lpf[x]; if (px == 0) { lpf[x] = px = x; primes.push_back(x); } for (int i = 2;; ++i) { int q = primes[i]; int y = q * x; if (y > n) break; lpf[y] = q; if (q == px) break; } } for (; x <= n; x += add_next, add_next ^= 0x2 ^ 4) { if (lpf[x] == 0) { lpf[x] = x; primes.push_back(x); } } return {move(primes), move(lpf)}; } constexpr int PSIZE = 1000000; auto [primes, lpf] = primes_lpf(PSIZE); vector& divisors_lv(int x) { int ps[10], cs[10]; int sz = 0; while (x != 1) { int p = lpf[x], c = 0; do {x /= p; c++;} while (x % p == 0); ps[sz] = p; cs[sz] = c; sz++; } static vector res; res.clear(); res.emplace_back(1); for (int i = 0; i < sz; i++) { int p = ps[i], ptr = 0; for (int t = cs[i] * res.size(); t > 0; t--) res.emplace_back(res[ptr++] * p); } return res; } template > struct TopK { int sz = 0; pair v[k]; void add(I value, L label) { if (auto it = ranges::find(v, v + sz, label, &pair::second); it != v + sz) { if (Comp{}(value, it->first)) { auto ins = ranges::upper_bound(v, it, value, Comp{}, &pair::first); move_backward(ins, it, it + 1); *ins = pair(move(value), move(label)); } return; } auto ins = ranges::upper_bound(v, v + sz, value, Comp{}, &pair::first); if (ins == v + k) return; auto it = sz == k ? v + k - 1 : v + sz++; move_backward(ins, it, it + 1); *ins = pair(move(value), move(label)); } auto begin() { return v; } auto end() { return v + sz; } auto& operator[](int i) { return v[i]; } }; } int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; VI a; ll ans = 0; rep(i, n) { int x; cin >> x; static bool used[200010]; if (used[x]) { ans += x; } else { used[x] = true; a.emplace_back(x); } } n = a.size(); dsu uf(n); vector> cand(200010); while (uf.size(0) != n) { rep(i, 200010) cand[i].sz = 0; rep(i, n) { int l = uf.leader(i); for (int d : divisors_lv(a[i])) { cand[d].add(a[i], l); } } constexpr long long INF = 1002003004005006007; vector> todo; for (auto& g : uf.groups()) { int l = uf.leader(g[0]); ll mn = INF; int lopt = -1; for (int i : g) for (int d : divisors_lv(a[i])) { for (auto [val, lb] : cand[d]) if (lb != l) { if (chmin(mn, (ll)val * a[i] / d)) lopt = lb; break; } } if (lopt != -1 && !uf.same(l, lopt)) { ans += mn; uf.merge(l, lopt); } } } cout << ans << '\n'; }