#pragma GCC optimize("Ofast") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long int ll; typedef unsigned long long ull; mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count()); ll myRand(ll B) { return (ull)rng() % B; } inline double time() { return static_cast(chrono::duration_cast(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9; } struct UnionFind{ vector par,num; UnionFind(int n):par(n),num(n,1){ iota(par.begin(),par.end(),0); //include } int find(int v){ return (par[v]==v)?v:(par[v]=find(par[v])); } void unite(int u,int v){ u=find(u),v=find(v); if(u==v)return; if(num[u]> n; vector a(n); for(int i=0;i> a[i]; } sort(a.begin(), a.end()); int m = a.back(); vector ex(m+1); ll res = 0; for(int i=0;i>> v; for(int g=1;g<=m;g++){ int f = -1; for(int i=g;i<=m;i+=g){ if(ex[i]){ if(f == -1){ f = i; } else{ v.push_back({(ll)f*(ll)i/g, {f,i}}); } } } } sort(v.begin(), v.end()); for(auto p:v){ int x = p.second.first, y = p.second.second; if(!uf.same(x,y)){ uf.unite(x,y); res += p.first; } } cout << res << endl; }