#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;
pair<int,int> vis2[10000005];
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]]}});}
int mn=1000000000;
int ind=-1;
for(int j=a[i];j<=10000000;j+=a[i]){
if(!vis[j])vis[j]=i;
if(vis2[j].fi!=0 && j!=a[i]){if(vis2[j-a[i]].se!=vis2[j].se || j-a[i]==a[i])v.pb({vis2[j].fi-j,{i,vis2[j].se}});continue;}
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(j!=a[i])vis2[j]=*it;
}
}
}
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;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:49:7: warning: unused variable 'mn' [-Wunused-variable]
49 | int mn=1000000000;
| ^~
sirni.cpp:50:7: warning: unused variable 'ind' [-Wunused-variable]
50 | int ind=-1;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
23 ms |
38544 KB |
Output is correct |
2 |
Incorrect |
407 ms |
146960 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
265 ms |
44052 KB |
Output is correct |
2 |
Incorrect |
1390 ms |
319440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
40536 KB |
Output is correct |
2 |
Correct |
14 ms |
19808 KB |
Output is correct |
3 |
Correct |
35 ms |
62356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1561 ms |
83428 KB |
Output is correct |
2 |
Correct |
4566 ms |
156256 KB |
Output is correct |
3 |
Correct |
2413 ms |
106968 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
189 ms |
55236 KB |
Output is correct |
2 |
Incorrect |
3039 ms |
107440 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3067 ms |
107260 KB |
Output is correct |
2 |
Execution timed out |
5022 ms |
157092 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1761 ms |
54276 KB |
Output is correct |
2 |
Execution timed out |
5022 ms |
156560 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
473 ms |
152228 KB |
Output is correct |
2 |
Execution timed out |
5056 ms |
521384 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
517 ms |
152488 KB |
Output is correct |
2 |
Runtime error |
1772 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
125892 KB |
Output is correct |
2 |
Runtime error |
1863 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |