#include <bits/stdc++.h>
using namespace std;
#define all(ac) ac.begin(),ac.end()
#define task "tet"
#define int long long
#define fi first
#define se second
#define db long double
const int N = 1e5+1;
int n, p[N];
namespace sub1 {
struct DSU {
int n;
vector <int> lab;
DSU(int n): n(n), lab(n+1, -1) {}
int get(int u) {return lab[u] < 0 ? u : lab[u] = get(lab[u]);}
bool join(int a, int b) {
a = get(a), b = get(b);
if(a != b) {
if(lab[a] < lab[b]) swap(a,b);
lab[a] += lab[b], lab[b] = a;
return 1;
}
return 0;
}
};
struct node {
int x, y, z;
bool operator <(const node o) const {
return x < o.x;
}
};
void solve() {
int ans = 0;
vector <node> vt;
for(int i=1;i<=n;i++) {
for(int j=1;j<i;j++) vt.emplace_back(node{min(p[i]%p[j], p[j] % p[i]),i,j});
}
sort(all(vt));
int cnt = 0;
DSU dsu(n);
for(auto e:vt) {
if(dsu.join(e.y, e.z)) ans += e.x, cnt++;
if(cnt == n-1) break;
}
cout << ans << '\n';
return;
}
};
int32_t main() {
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n;
for(int i=1;i<=n;i++) cin >> p[i];
sub1::solve();
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... |