#include<bits/stdc++.h>
using namespace std;
const int maxN=(1e5)+5;
const int maxV=(1e7)+5;
const int inf=INT_MAX;
struct tip
{
int x,y,cost;
bool operator<(const tip& other)
{
return cost<other.cost;
}
};
int t[maxN];
inline int getRoot(int x)
{
//getting the root
int y=x;
while(t[y]>0) y=t[y];
//road compression
int z=y;
y=x;
while(y!=z)
{
int aux=t[y];
t[y]=z;
y=aux;
}
return y;
}
inline void unite(int x,int y)
{
int tx=getRoot(x);
int ty=getRoot(y);
if(tx<ty)
{
t[tx]+=t[ty];
t[ty]=tx;
}
else
{
t[ty]+=t[tx];
t[tx]=ty;
}
}
vector<tip> edges;
int n,v[maxN],cnt,M[maxV],nrm[maxN],x,y,cst,tx,ty;
long long sol;
inline int cost(int x,int y)
{
int xa,ya;
xa=M[x];
ya=M[y];
return min((xa%ya),(ya%xa));
}
int taken=0;
int main()
{
// freopen("sirni.in","r",stdin);
// freopen("sirni.out","w",stdout);
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&v[i]);
sort(v+1,v+n+1);
v[n+1]=inf;
for(int i=1;i<=n;i++)
{
if(i==1 || v[i]!=v[i-1]) cnt++;
M[cnt]=v[i];
nrm[i]=cnt;
}
for(int i=1;i<=cnt;i++)
{
int x=M[i];
if(x>=v[n]) continue;
int ind=upper_bound(v+1,v+n+1,x)-v;
edges.push_back({i,nrm[ind],cost(i,nrm[ind])});
int lst=nrm[ind];
for(int j=x+x;j<=v[n];j+=x)
{
if(j>v[n]) continue;
ind=lower_bound(v+1,v+n+1,j)-v;
if(nrm[ind]!=lst) edges.push_back({i,nrm[ind],cost(i,nrm[ind])});
lst=nrm[ind];
}
}
sort(edges.begin(),edges.end());
//for(auto it:edges)
// printf("%d %d% d\n",it.x,it.y,it.cost);
for(int i=1;i<=cnt;i++)
t[i]=-1;
for(auto it:edges)
{
if(taken==cnt-1) break;
x=it.x;
y=it.y;
cst=it.cost;
tx=getRoot(x);
ty=getRoot(y);
if(tx!=ty)
{
taken++;
sol+=1LL*cst;
unite(tx,ty);
}
}
printf("%lld\n",sol);
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:78:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&n);
~~~~~^~~~~~~~~
sirni.cpp:81:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d",&v[i]);
~~~~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
97 ms |
3596 KB |
Output is correct |
3 |
Correct |
5 ms |
636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
864 ms |
1220 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
504 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
4 ms |
504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
220 ms |
14092 KB |
Output is correct |
2 |
Correct |
768 ms |
50872 KB |
Output is correct |
3 |
Correct |
321 ms |
26184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
3692 KB |
Output is correct |
2 |
Correct |
428 ms |
50656 KB |
Output is correct |
3 |
Correct |
216 ms |
14168 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
500 ms |
50860 KB |
Output is correct |
2 |
Correct |
1032 ms |
100308 KB |
Output is correct |
3 |
Correct |
301 ms |
26184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
74 ms |
7008 KB |
Output is correct |
2 |
Correct |
1061 ms |
99996 KB |
Output is correct |
3 |
Correct |
295 ms |
26308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
282 ms |
26312 KB |
Output is correct |
2 |
Correct |
4875 ms |
395808 KB |
Output is correct |
3 |
Correct |
331 ms |
26184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
290 ms |
26284 KB |
Output is correct |
2 |
Execution timed out |
5065 ms |
395672 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
3820 KB |
Output is correct |
2 |
Execution timed out |
5030 ms |
395648 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |