#include <bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
#pragma GCC optimize("O3,unroll-loops")
using ll = long long;
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 0;
if(size[u] < size[v]) swap(u, v);
size[u] += size[v];
par[v] = u;
comp--;
return 1;
}
};
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;
set<int> s;
for(int &x : v) s.insert(x);
v = vector<int>(s.begin(), s.end());
if(v[0] == 1 || v.size() == 1) {
cout << 0 << '\n';
return 0;
}
DSU dsu; dsu.config(n);
vector<Edge> edges;
n = v.size();
int mx = v[n-1];
for(int i=0; i<n; i++) {
int j = i;
for(int val=v[i]; val<=mx; val+=v[i]) {
int last = j;
while(j + 1 < n && v[j] < val) j++;
if(v[j] == v[i]) j++;
if(j != n && last != j) edges.push_back({ i, j, v[j] % v[i] });
}
}
sort(edges.begin(), edges.end());
for(Edge &e : edges)
if(dsu.uni(e.a, e.b)) ans += e.w;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
22 ms |
4620 KB |
Output is correct |
3 |
Correct |
2 ms |
796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
95 ms |
1540 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1010 ms |
19832 KB |
Output is correct |
2 |
Correct |
1064 ms |
56280 KB |
Output is correct |
3 |
Correct |
699 ms |
30776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
4344 KB |
Output is correct |
2 |
Correct |
231 ms |
51992 KB |
Output is correct |
3 |
Correct |
677 ms |
18268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1160 ms |
57100 KB |
Output is correct |
2 |
Correct |
1248 ms |
105716 KB |
Output is correct |
3 |
Correct |
930 ms |
32272 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
9212 KB |
Output is correct |
2 |
Correct |
1161 ms |
104228 KB |
Output is correct |
3 |
Correct |
779 ms |
31904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1050 ms |
31028 KB |
Output is correct |
2 |
Correct |
3162 ms |
401660 KB |
Output is correct |
3 |
Correct |
1062 ms |
32040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1071 ms |
31796 KB |
Output is correct |
2 |
Correct |
4173 ms |
399624 KB |
Output is correct |
3 |
Correct |
1061 ms |
30952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
4864 KB |
Output is correct |
2 |
Correct |
4054 ms |
398792 KB |
Output is correct |
3 |
Correct |
811 ms |
30980 KB |
Output is correct |