#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],mx[10000005],ind[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];
mx[a[i]]=a[i];
}
for(int i=10000000-1;i>=1;i--){
if(!mx[i])mx[i]=mx[i+1];
}
sort(a+1,a+n+1);
FOR{
if(!ind[a[i]])ind[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]]}});}
if(mx[a[i]+1])v.pb({mx[a[i]+1]-a[i],{i,ind[mx[a[i]+1]]}});
for(int j=a[i]*2;j<=10000000;j+=a[i]){
vis[j]=i;
if(mx[j] && mx[j]!=mx[j+a[i]])v.pb({mx[j]-j,{i,ind[mx[j]]}});
}
}
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 |
32 ms |
86096 KB |
Output is correct |
2 |
Correct |
80 ms |
90312 KB |
Output is correct |
3 |
Correct |
33 ms |
88340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
85076 KB |
Output is correct |
2 |
Correct |
328 ms |
85840 KB |
Output is correct |
3 |
Correct |
33 ms |
88404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
86556 KB |
Output is correct |
2 |
Correct |
35 ms |
84988 KB |
Output is correct |
3 |
Correct |
34 ms |
88400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
312 ms |
115120 KB |
Output is correct |
2 |
Correct |
1028 ms |
140456 KB |
Output is correct |
3 |
Correct |
447 ms |
114360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
81 ms |
91588 KB |
Output is correct |
2 |
Correct |
588 ms |
137752 KB |
Output is correct |
3 |
Correct |
409 ms |
112812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
628 ms |
139112 KB |
Output is correct |
2 |
Correct |
1249 ms |
188060 KB |
Output is correct |
3 |
Correct |
368 ms |
115220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
275 ms |
93120 KB |
Output is correct |
2 |
Correct |
1312 ms |
188012 KB |
Output is correct |
3 |
Correct |
410 ms |
115072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
147632 KB |
Output is correct |
2 |
Correct |
3972 ms |
517592 KB |
Output is correct |
3 |
Correct |
290 ms |
147900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
146872 KB |
Output is correct |
2 |
Execution timed out |
5046 ms |
515836 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
120008 KB |
Output is correct |
2 |
Execution timed out |
5034 ms |
510860 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |