#include <bits/stdc++.h>
using namespace std;
typedef long long lo;
#define fi first
#define se second
#define endl "\n"
#define pb push_back
#define fio() ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define FOR for(int i=1;i<=n;i++)
#define mid ((start+end)/2)
#define ort ((bas+son)/2)
#define _ << " " <<
const lo inf = 1000000000;
const lo li = 500005;
const lo mod = 1000000007;
int n,m,a[li],k,flag,t,fa[li],vis[10000005];
long long cev;
string s;
vector<pair<int,pair<int,int>>> v;
set<pair<int,int>> st;
inline int dsu(int x){
if(x==fa[x])return x;
return fa[x]=dsu(fa[x]);
}
int main(void){
//~ fio();
cin>>n;
FOR{
fa[i]=i;
cin>>a[i];
}
sort(a+1,a+n+1);
if(a[1]==1){cout<<"0\n";return 0;}
FOR st.insert({a[i],i});
FOR{
if(a[i]==a[i-1]){
v.pb({0,{i-1,i}});
continue;
}
if(vis[a[i]]){v.pb({0,{i,vis[a[i]]}});}
flag=0;
int at=a[i];
for(int j=2;j*j<=at;j++){
if(at%j && vis[at]==0){flag=1;break;}
while(at%j){at/=j;}
}
if(at && vis[at]==0)flag=1;
if(flag==0)continue;
int mn=1000000000;
int ind=-1;
for(int j=a[i];j<=10000000;j+=a[i]){
if(!vis[j])vis[j]=i;
auto it=st.lower_bound({j,-inf});
if(j==a[i])it=st.upper_bound({j,inf});
if(it!=st.end()){
v.pb({it->fi-j,{it->se,i}});
if(it->fi-j<mn){mn=it->fi-j;ind=it->se;}
}
}
//~ cout<<i<<" ()() "<<endl;
if(~ind)v.pb({mn,{ind,i}});
}
int last=0,ind=0,cnt=1;
for(int i=1;i<=n;i++){
while(cnt<a[i]){
//~ cout<<vis[cnt]<<" () "<<a[i]<<endl;
//~ if(vis[cnt] && vis[cnt]!=i){last=cnt;ind=vis[cnt];}
if(vis[cnt]){last=cnt;ind=vis[cnt];}
cnt++;
}
if(ind)v.pb({a[i]-last,{ind,i}});
}
sort(v.begin(),v.end());
for(auto go:v){
int co=go.fi;
int x=go.se.fi;
int y=go.se.se;
//~ cout<<x<<" () "<<y<<" () "<<co<<endl;
if(dsu(x)==dsu(y))continue;
fa[dsu(x)]=dsu(y);
cev+=co;
}
cout<<cev<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
20316 KB |
Output is correct |
2 |
Correct |
429 ms |
92892 KB |
Output is correct |
3 |
Correct |
25 ms |
39508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
238 ms |
41780 KB |
Output is correct |
2 |
Correct |
2168 ms |
436364 KB |
Output is correct |
3 |
Correct |
32 ms |
43220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
21336 KB |
Output is correct |
2 |
Correct |
11 ms |
12096 KB |
Output is correct |
3 |
Correct |
19 ms |
29464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1438 ms |
73464 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
181 ms |
45508 KB |
Output is correct |
2 |
Correct |
2677 ms |
98064 KB |
Output is correct |
3 |
Correct |
2098 ms |
73236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2512 ms |
72440 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1336 ms |
50380 KB |
Output is correct |
2 |
Correct |
2540 ms |
96420 KB |
Output is correct |
3 |
Correct |
1928 ms |
72732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
458 ms |
71856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
520 ms |
71852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
46792 KB |
Output is correct |
2 |
Execution timed out |
5059 ms |
441780 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |