//Make CSP great again //Vengeance #include #define TASK "TESTCODE" using namespace std; const int N = 1e6, M = 1e5; int lab[N + 1]; int last[M + 1]; int FindSet(int u) { return lab[u] < 0 ? u : lab[u] = FindSet(lab[u]); } bool unite(int u, int v) { u = FindSet(u); v = FindSet(v); if (u == v) { return false; } if (lab[u] > lab[v]) { swap(u, v); } lab[u] += lab[v]; lab[v] = u; return true; } int rep[M + 1]; int n; long long res = 0; void read() { cin >> n; for (int i = 1; i <= n; ++ i) { int a; cin >> a; lab[i] = -1; if (rep[a]) { res += a; unite(rep[a], i); } rep[a] = i; } } void solve() { vector > total; for (int i = 1; i <= M; ++ i) { for (int j = i; j <= M; j += i) { if (rep[j]) { last[i] = j; break; } } } for (int i = 1; i <= M; ++ i) { if (!last[i]) { continue; } for (int j = i; j <= M; j += i) { if (rep[j]) { total.emplace_back(1LL * last[i] * j/__gcd(last[i], j), last[i], j); } } } for (auto [w, u, v] : total) { res += 1LL * w * unite(rep[u], rep[v]); } assert(lab[FindSet(1)] == -n); cout << res; } int main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); if (fopen(TASK".INP", "r")) { freopen(TASK".INP", "r", stdin); //freopen(TASK".OUT", "w", stdout); } int t = 1; bool typetest = false; if (typetest) { cin >> t; } for (int __ = 1; __ <= t; ++ __) { //cout << "Case " << __ << ": "; read(); solve(); } }