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>
#define ll long long
using namespace std;
struct edge{
int u, v, w;
bool operator<(edge other){
return w < other.w;
}
};
const int MAX = 1e3 + 1;
int _parent[MAX], _size[MAX];
void make(int i){
_parent[i] = i;
_size[i] = 1;
}
int find(int i){
if(_parent[i] == i) return i;
return _parent[i] = find(_parent[i]);
}
bool merge(int u, int v){
u = find(u), v = find(v);
if(u == v) return false;
if(_size[u] < _size[v]) swap(u, v);
_parent[v] = u;
_size[u] += _size[v];
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
vector<int> P(n + 1);
int g[n + 1][n + 1];
for(int i = 1; i <= n; i++) cin>>P[i], make(i);
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
g[i][j] = min(P[i] % P[j], P[j] % P[i]);
}
}
vector<edge> edges;
for(int i = 1; i <= n; i++){
for(int j = i + 1; j <= n; j++){
edges.push_back({i, j, g[i][j]});
}
}
sort(edges.begin(), edges.end());
int ans = 0;
for(auto e : edges){
if(merge(e.u, e.v)){
ans += e.w;
n--;
}
}
cout<<ans<<endl;
}
# | 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... |