#include<bits/stdc++.h>
using namespace std;
using ll = long long;
const ll N = 1e6 + 5;
const ll base = 37;
const ll inf = LLONG_MAX/4;
const ll mod = 998244353;
#define bit(x,y) ((x >> y) & 1)
ll p[N];
ll fp(ll x)
{
if(p[x] == 0) return x;
else
{
p[x] = fp(p[x]);
return p[x];
}
}
ll c;
ll hop(ll x, ll y)
{
ll px=fp(x);
ll py=fp(y);
if(px == py) return 0;
p[px] = py;
c--;
return 1;
}
int haha[10000005];
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
ll n;
cin >> n;
ll i,j;
vector<ll> a;
ll m = 0;
for(i = 1;i <= n;i++)
{
ll x;
cin >> x;
m = max(m,x);
if(!haha[x])
{
haha[x] = 1;
a.push_back(x);
haha[x] = ++c;
}
}
ll d = 0;
ll ans = 0;
while(true)
{
for(auto x : a)
{
for(ll y = d;y <= m;y += x)
{
if(haha[y])
{
ans += hop(haha[x],haha[y]) * d;
}
}
}
if(c == 1) break;
d++;
}
cout << ans;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |