This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include "bits/stdc++.h"
using namespace std;
int main(){
ios::sync_with_stdio(0);cin.tie(0);
int n;
cin>>n;
vector<int> a(n),parent(n);
for(auto &i:a)cin>>i;
for(int i=0;i<n;i++){
parent[i] =i;
}
auto find = [&](int x){
while(x != parent[x])x = parent[x];
return x;
};
auto unite = [&](int a,int b){
a = find(a);
b = find(b);
if(a != b){
parent[b] = a;
}
};
vector<pair<int,pair<int,int>>> edges;
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
edges.push_back({min(a[i]%a[j],a[j]%a[i]),{i,j}});
}
}
long long res = 0;
sort(edges.begin(),edges.end());
for(auto edge:edges){
int x = edge.second.first,b = edge.second.second;
if(find(x) != find(b)){
res += edge.first;
unite(x,b);
}
}
cout << res <<'\n';
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |