#include <bits/stdc++.h>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
//#define int long long
using namespace std;
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
const int mod = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e5 + 5;
const double eps = 1e-9;
struct DSU {
int n, comp;
vector<int> par, size;
void config(int _n) {
n = _n + 1;
comp = _n;
par.resize(n+1);
size.resize(n+1, 1);
for(int i=1; i<=n; i++) par[i] = i;
}
int get(int u) {
if(u == par[u]) return u;
return par[u] = get(par[u]);
}
bool uni(int u, int v) {
u = get(u), v = get(v);
if(u == v) return false;
if(size[u] < size[v]) swap(u, v);
size[u] += size[v];
par[v] = u;
comp--;
return true;
}
int getComp() { return comp; }
int getSize(int u) { return size[get(u)]; }
bool sameSet(int u, int v) { return get(u) == get(v); }
};
struct Edge {
int a, b, w;
bool operator<(Edge &e) { return w < e.w; }
};
int32_t main() {
ios_base::sync_with_stdio(false);
cout.tie(0); cin.tie(0);
int n;
ll ans = 0;
cin >> n;
vector<int> v(n);
for(int &x : v) cin >> x;
sort(all(v));
DSU dsu; dsu.config(n);
vector<Edge> edges;
for(int i=0; i<n; i++)
for(int j=i+1; j<n; j++) edges.push_back({ i, j, v[j] % v[i] });
sort(all(edges));
for(Edge &e : edges)
if(dsu.uni(e.a, e.b)) ans += e.w;
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
6856 KB |
Output is correct |
2 |
Correct |
35 ms |
8396 KB |
Output is correct |
3 |
Correct |
37 ms |
7112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
27 ms |
8392 KB |
Output is correct |
2 |
Correct |
27 ms |
8648 KB |
Output is correct |
3 |
Correct |
38 ms |
7368 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
39 ms |
7112 KB |
Output is correct |
2 |
Correct |
32 ms |
8136 KB |
Output is correct |
3 |
Correct |
37 ms |
8140 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
801 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
653 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
647 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
618 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
628 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
619 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
608 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |