# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
393448 | 2021-04-23T13:33:34 Z | ngpin04 | Sirni (COCI17_sirni) | C++14 | 0 ms | 0 KB |
#include <bits/stdc++.h> #define fi first #define se second #define mp make_pair using namespace std; const int N = 1e7 + 5; vector <pair <int, int>> edge[N]; int a[N]; int par[N]; int n; bool mini(int &a, int b) { if (a > b) { a = b; return true; } return false; } int getpar(int u) { return (par[u] < 0) ? u : (par[u] = getpar(par[u])); } bool join(int u, int v) { u = getpar(u); v = getpar(v); if (u == v) return false; if (par[u] > par[v]) swap(u, v); par[u] += par[v]; par[v] = u; return true; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n; for (int i = 1; i <= n; i++) cin >> a[i]; sort(a + 1, a + n + 1); n = unique(a + 1, a + n + 1) - (a + 1); for (int i = 1; i <= n; i++) { p[a[i]] = a[i]; val[a[i]] = 1e9; } int mx = a[n]; for (int i = mx - 1; i >= 1; i--) if (!p[i]) p[i] = p[i + 1]; for (int i = 1; i <= n; i++) { int x = a[i]; if (i < n) edge[p[x + 1] - x].push_back(mp(x, p[x + 1])); for (int j = 2 * x; j <= mx; j += x) { // cout << p[j] << " " << j << "\n"; edge[p[j] - j].push_back(mp(x, p[j])); } } for (int i = 1; i <= mx; i++) par[i] = -1; long long ans = 0; for (int i = 0; i <= mx; i++) for (pair <int, int> e : edge[i]) ans += i * join(e.fi, e.se); cout << ans; return 0; }