#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;
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});
}
}
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];
if(union_set(u,v))ans+=w;
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
24292 KB |
Output is correct |
2 |
Correct |
155 ms |
26628 KB |
Output is correct |
3 |
Correct |
156 ms |
27460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
26628 KB |
Output is correct |
2 |
Correct |
208 ms |
27820 KB |
Output is correct |
3 |
Correct |
163 ms |
27828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
204 ms |
27640 KB |
Output is correct |
2 |
Correct |
109 ms |
22528 KB |
Output is correct |
3 |
Correct |
170 ms |
28156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
868 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
544 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
584 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
603 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
698 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
652 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
561 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |