#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define rep(i,a,b) for(i=a;i<b;i++)
ll nex[20000002];
ll sz[20000002],vis[20000002];
ll p[20000000];
ll ans=0;
int par(int x)
{
if(p[x]==x)
return x;
p[x]=par(p[x]);
return p[x];
}
void un(int x,int y,ll cost)
{
//cout<<x<<' '<<y<<' '<<cost<<endl;
x=par(x);
y=par(y);
if(x==y)
{
return ;
}
ans+=cost;
if(sz[x]<sz[y])
{
swap(x,y);
}
sz[x]+=sz[y];
sz[y]=0;
p[y]=x;
}
int main()
{
int n;
cin>>n;
ll a[n];
for(int i=0;i<n;i++)
{
cin>>a[i];
p[a[i]]=a[i];
sz[a[i]]=1;
nex[a[i]]=a[i];
}
for(int i=10000002;i>=0;i--)
{
if(nex[i]==i)
{
continue;
}
nex[i]=nex[i+1];
}
vector<pair<ll,ll> >v[10000002];
for(int i=1;i<=1e7;i++)
{
if(nex[i]==i&&vis[i]==0)
{
vis[i]=1;
for(int y=i;y<=1e7;y+=i)
{
vis[y]=1;
// if(nex[y]!=0)
if(nex[y]==y)
{
v[0].push_back({i,y});
}
if(nex[y+1]!=0)
v[nex[y+1]%i].push_back({i,nex[y+1]});
}
}
}
for(ll i=0;i<1e7;i++)
{
for(auto y:v[i])
{
un(y.first,y.second,i);
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
339916 KB |
Output is correct |
2 |
Correct |
378 ms |
453168 KB |
Output is correct |
3 |
Correct |
239 ms |
377712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
330 ms |
391964 KB |
Output is correct |
2 |
Correct |
797 ms |
689916 KB |
Output is correct |
3 |
Correct |
250 ms |
401188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
341732 KB |
Output is correct |
2 |
Correct |
233 ms |
327772 KB |
Output is correct |
3 |
Correct |
241 ms |
354856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
452 ms |
423788 KB |
Output is correct |
2 |
Correct |
564 ms |
440908 KB |
Output is correct |
3 |
Correct |
532 ms |
446416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
297 ms |
411016 KB |
Output is correct |
2 |
Correct |
520 ms |
426220 KB |
Output is correct |
3 |
Correct |
539 ms |
414684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
553 ms |
434532 KB |
Output is correct |
2 |
Correct |
555 ms |
451648 KB |
Output is correct |
3 |
Correct |
447 ms |
431888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
432 ms |
399956 KB |
Output is correct |
2 |
Correct |
621 ms |
444996 KB |
Output is correct |
3 |
Correct |
513 ms |
443392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
572288 KB |
Output is correct |
2 |
Runtime error |
710 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
550 ms |
582252 KB |
Output is correct |
2 |
Runtime error |
675 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
304 ms |
496948 KB |
Output is correct |
2 |
Correct |
1104 ms |
754656 KB |
Output is correct |
3 |
Correct |
564 ms |
454864 KB |
Output is correct |