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
#define pii pair<int, int>
using namespace std;
const int MAX = 1e5 + 5;
int maxVal = 0;
set<int> s;
struct edge{
int u, v, weight;
bool operator<(edge e){
return weight < e.weight;
}
};
int par[10000007];
int findPar(int u){
if(par[u] < 0) return u;
return par[u] = findPar(par[u]);
}
bool setUnion(int u, int v){
u = findPar(u);
v = findPar(v);
if(u == v) return false;
if(-par[u] < -par[v]) swap(u, v);
par[u] += par[v];
par[v] = u;
return true;
}
int main(){
int n; cin >> n;
for (int i = 0; i < n; i++)
{
int a; cin >> a;
s.insert(a);
}
maxVal = *(prev(s.end()));
vector<edge> edges;
for(int a:s){
if(a == maxVal) continue;
for(int j = a; j <= maxVal; j += a){
int b = 0;
if(j == a){
b = *s.upper_bound(j);
}
else{
b = *s.lower_bound(j);
}
if((b / a) == (j / a)){
edges.push_back({a, b, b % a});
}
}
}
fill(par, par + maxVal + 1, -1);
sort(edges.begin(), edges.end());
ll cost = 0;
for(edge& e:edges){
if(setUnion(e.u, e.v)){
cost += e.weight;
}
}
cout << cost << '\n';
}
# | 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... |