#include #include using namespace std; using int64 = long long; const int inf = (1 << 30) - 1; #line 2 "graph/mst/kruskal.hpp" #line 2 "graph/graph-template.hpp" /** * @brief Graph Template(グラフテンプレート) */ template< typename T = int > struct Edge { int from, to; T cost; int idx; Edge() = default; Edge(int from, int to, T cost = 1, int idx = -1) : from(from), to(to), cost(cost), idx(idx) {} operator int() const { return to; } }; template< typename T = int > struct Graph { vector< vector< Edge< T > > > g; int es; Graph() = default; explicit Graph(int n) : g(n), es(0) {} size_t size() const { return g.size(); } void add_directed_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es++); } void add_edge(int from, int to, T cost = 1) { g[from].emplace_back(from, to, cost, es); g[to].emplace_back(to, from, cost, es++); } void read(int M, int padding = -1, bool weighted = false, bool directed = false) { for(int i = 0; i < M; i++) { int a, b; cin >> a >> b; a += padding; b += padding; T c = T(1); if(weighted) cin >> c; if(directed) add_directed_edge(a, b, c); else add_edge(a, b, c); } } inline vector< Edge< T > > &operator[](const int &k) { return g[k]; } inline const vector< Edge< T > > &operator[](const int &k) const { return g[k]; } }; template< typename T = int > using Edges = vector< Edge< T > >; #line 1 "structure/union-find/union-find.cpp" /** * @brief Union-Find * @docs docs/union-find.md */ struct UnionFind { vector< int > data; UnionFind() = default; explicit UnionFind(size_t sz) : data(sz, -1) {} bool unite(int x, int y) { x = find(x), y = find(y); if(x == y) return false; if(data[x] > data[y]) swap(x, y); data[x] += data[y]; data[y] = x; return true; } int find(int k) { if(data[k] < 0) return (k); return data[k] = find(data[k]); } int size(int k) { return -data[find(k)]; } bool same(int x, int y) { return find(x) == find(y); } vector< vector< int > > groups() { int n = (int) data.size(); vector< vector< int > > ret(n); for(int i = 0; i < n; i++) { ret[find(i)].emplace_back(i); } ret.erase(remove_if(begin(ret), end(ret), [&](const vector< int > &v) { return v.empty(); })); return ret; } }; #line 5 "graph/mst/kruskal.hpp" /** * @brief Kruskal(最小全域木) * @docs docs/kruskal.md */ template< typename T > struct MinimumSpanningTree { T cost; Edges< T > edges; }; template< typename T > MinimumSpanningTree< T > kruskal(Edges< T > &edges, int V) { sort(begin(edges), end(edges), [](const Edge< T > &a, const Edge< T > &b) { return a.cost < b.cost; }); UnionFind tree(V); T total = T(); Edges< T > es; for(auto &e: edges) { if(tree.unite(e.from, e.to)) { es.emplace_back(e); total += e.cost; } } return {total, es}; } int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int N; cin >> N; vector< int > A(N); for(auto &a: A) cin >> a; vector< vector< int > > v(100001); for(int i = 0; i < N; i++) { v[A[i]].emplace_back(i); } vector< Edge< int64 > > es; vector< int > eval(N); for(int i = 100000; i >= 1; i--) { int low = inf, id = -1; for(int j = i; j <= 100000; j += i) { if(not v[j].empty()) { low = j; id = v[j].front(); break; } } if(low == inf) continue; for(int j = i; j <= 100000; j += i) { for(auto &p: v[j]) { es.emplace_back(id, p, 1LL * low * j / i); } if(v[j].size() >= 2) v[j].resize(1); } } cout << kruskal(es, N).cost << "\n"; }