// Practice makes perfect
#include <bits/stdc++.h>
#include <set>
using namespace std;
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0);
#define file ""
#define ll long long
const int maxn = 1e5 + 5;
int n, p[maxn], lab[maxn];
set<int> s;
ll ans = 0;
struct Edge {
int u, v, w;
bool operator < (Edge &e) {
return w < e.w;
}
};
vector<Edge> edges;
int Find(int u) {
return lab[u] < 0 ? u : lab[u] = Find(lab[u]);
}
bool unite(int a, int b) {
a = Find(a);
b = Find(b);
if (a == b)
return 0;
if (lab[a] > lab[b])
swap(a, b);
lab[a] += lab[b];
lab[b] = a;
return 1;
}
int main() {
fastIO
//freopen(file".inp", "r", stdin);
//freopen(file".out", "w", stdout);
cin >> n;
for (int i = 0; i < n; ++i) {
int x;
cin >> x;
s.insert(x);
}
vector<int> v = vector<int>(s.begin(), s.end());
if (v[0] == 1 || v.size() == 1) {
cout << 0;
return 0;
}
for (int i = 0; i < n; ++i)
lab[i] = -1;
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) {
Edge e;
e.u = i;
e.v = j;
e.w = v[j] % v[i];
edges.push_back(e);
}
}
}
sort(edges.begin(), edges.end());
for (Edge &e : edges)
if (unite(e.u, e.v))
ans += e.w;
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
28 ms |
3540 KB |
Output is correct |
3 |
Correct |
2 ms |
792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
94 ms |
1336 KB |
Output is correct |
3 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1274 ms |
18064 KB |
Output is correct |
2 |
Correct |
1365 ms |
54596 KB |
Output is correct |
3 |
Correct |
891 ms |
29352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
4240 KB |
Output is correct |
2 |
Correct |
222 ms |
50636 KB |
Output is correct |
3 |
Correct |
846 ms |
17256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1448 ms |
55268 KB |
Output is correct |
2 |
Correct |
1490 ms |
104144 KB |
Output is correct |
3 |
Correct |
1195 ms |
30128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
177 ms |
9660 KB |
Output is correct |
2 |
Correct |
1421 ms |
103144 KB |
Output is correct |
3 |
Correct |
1018 ms |
29612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1355 ms |
30500 KB |
Output is correct |
2 |
Correct |
2998 ms |
399820 KB |
Output is correct |
3 |
Correct |
1325 ms |
30632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1379 ms |
30572 KB |
Output is correct |
2 |
Correct |
3697 ms |
399092 KB |
Output is correct |
3 |
Correct |
1317 ms |
31144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
4816 KB |
Output is correct |
2 |
Correct |
3539 ms |
399268 KB |
Output is correct |
3 |
Correct |
1053 ms |
30388 KB |
Output is correct |