#include <bits/stdc++.h>
#define int long long
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=1e5+5;
int p[N];
int find_set(int v){
if(p[v]==v)return v;
return p[v]=find_set(p[v]);
}
bool union_set(int u,int v){
u=find_set(u);
v=find_set(v);
if(u==v)return false;
p[u]=v;
return true;
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n;
cin>>n;
vector <int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
vector < vector <int> > v;
sort(all(a));
if(n<=1000){
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
int x=min({a[i]%a[j],a[j]%a[i]});
v.pb({x,i,j});
}
}
}
else{
for(int i=0;i<n;i++){
if(i-1>=0 && a[i]==a[i-1])continue;
for(int j=a[i];j<=1e6;j+=a[i]){
auto it=lower_bound(all(a),j);
if(j==a[i])it=upper_bound(all(a),j);
if(it!=a.end()){
int pos=it-a.begin();
v.pb({min(a[pos]%a[i],a[i]%a[pos]),pos,i});
}
}
}
}
sort(all(v));
for(int i=0;i<n;i++)p[i]=i;
int ans=0;
for(auto x : v){
int w=x[0];
int u=x[1];
int v=x[2];
//cout<<w<<" "<<u<<" "<<v<<"\n";
if(union_set(u,v))ans+=w;
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
26108 KB |
Output is correct |
2 |
Correct |
141 ms |
27428 KB |
Output is correct |
3 |
Correct |
109 ms |
28012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
26684 KB |
Output is correct |
2 |
Correct |
137 ms |
27816 KB |
Output is correct |
3 |
Correct |
117 ms |
27652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
120 ms |
27936 KB |
Output is correct |
2 |
Correct |
75 ms |
22908 KB |
Output is correct |
3 |
Correct |
108 ms |
27832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
692 ms |
83840 KB |
Output is correct |
2 |
Correct |
2760 ms |
231284 KB |
Output is correct |
3 |
Correct |
1035 ms |
166900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
11788 KB |
Output is correct |
2 |
Correct |
1354 ms |
166652 KB |
Output is correct |
3 |
Correct |
642 ms |
84276 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1551 ms |
166472 KB |
Output is correct |
2 |
Correct |
4138 ms |
329304 KB |
Output is correct |
3 |
Correct |
1034 ms |
109080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
187 ms |
22260 KB |
Output is correct |
2 |
Correct |
3900 ms |
338060 KB |
Output is correct |
3 |
Correct |
1022 ms |
113632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
68 ms |
8072 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
73 ms |
12288 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
9 ms |
1428 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |