#include<bits/stdc++.h>
using namespace std;
#define mod 1000000007
#define nmax 100007
#define fi first
#define se second
#define ll int
ll t=1,n,m=0,i=0,j=0,d=0,x=0,k=0,y=0,z,a[nmax],f[10000003],h[10000003],cnt=0,pa[nmax],sz[nmax];
long long kq=0;
struct hm
{
ll a,b,c;
}v[nmax];
bool cmp(hm a,hm b)
{
return a.c<b.c;
}
ll findset(ll u)
{
if(pa[u]==u)return u;
return pa[u]=findset(pa[u]);
}
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n;
for(i=1;i<=n;++i)
{
cin>>a[i];
x=max(x,a[i]);
if(f[a[i]])
{
i--;
n--;
continue;
}
sz[i]=1;
pa[i]=i;
f[a[i]]=a[i];
}
sort(a+1,a+n+1);
for(i=x;i>=1;--i)
{
if(!f[i])f[i]=f[i+1];
}
d=0;
for(i=1;i<=x;++i)
{
if(f[i]!=f[i-1])d++;
h[i]=d;
}
for(i=1;i<=n;++i)
{
for(j=a[i];j<=x;j+=a[i])
{
y=j;
if(j==a[i])y++;
if(y>x)break;
if(f[y]<j+a[i])
{
v[++cnt].a=i;
v[cnt].b=h[y];
v[cnt].c=f[y]%a[i];
}
}
}
sort(v+1,v+cnt+1,cmp);
kq=0;
for(i=1;i<=cnt;++i)
{
x=findset(v[i].a);
y=findset(v[i].b);
if(x!=y)
{
if(sz[x]<sz[y])swap(x,y);
sz[x]+=sz[y];
pa[y]=x;
kq+=v[i].c;
}
}
cout<<kq;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
80472 KB |
Output is correct |
2 |
Runtime error |
725 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2640 KB |
Output is correct |
2 |
Correct |
173 ms |
80404 KB |
Output is correct |
3 |
Correct |
44 ms |
80456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
80456 KB |
Output is correct |
2 |
Correct |
47 ms |
80200 KB |
Output is correct |
3 |
Correct |
49 ms |
80356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
776 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
732 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
144 ms |
33612 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
23 ms |
5496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
822 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
840 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
740 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |