#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
#define mp make_pair
#define f first
#define s second
#define ii pair<int, int>
int pa[100000], sz[100000];
int findSet(int x) {
return pa[x] == x ? x : pa[x] = findSet(pa[x]);
}
void unite(int a, int b) {
a = findSet(a); b = findSet(b);
if (sz[a] > sz[b]) swap(a, b);
pa[a] = b;
sz[b] += sz[a];
}
int main() {
cin.tie(0); ios_base::sync_with_stdio(false);
int n; cin >> n;
set<int> nums;
for (int i = 0; i < n; i++) {
int p; cin >> p;
nums.insert(p);
}
map<int, int> idx;
int ctr = 0;
for (int num : nums) {
idx[num] = ctr++;
}
for (int i = 0; i < n; i++) {
sz[i] = 1;
pa[i] = i;
}
vector<pair<int, ii>> edges;
for (int num : nums) {
int prev = -1;
for (int i = num; i <= 1e7; i += num) {
auto it = i == num ? nums.upper_bound(i) : nums.lower_bound(i);
if (it != nums.end()) {
if (prev == *it) continue;
prev = *it;
edges.pb(mp((*it) % num, mp(idx[num], idx[*it])));
} else {
break;
}
}
/*
for (int num2 : nums) {
if (num < num2)
edges.pb(mp(num2%num, mp(idx[num], idx[num2])));
}
*/
}
sort(edges.begin(), edges.end());
ll ans = 0;
for (auto e : edges) {
if (e.s.s >= ctr) continue;
if (findSet(e.s.f) != findSet(e.s.s)) {
ans += e.f;
unite(e.s.f, e.s.s);
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
768 KB |
Output is correct |
2 |
Correct |
91 ms |
3692 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
640 KB |
Output is correct |
2 |
Correct |
553 ms |
1384 KB |
Output is correct |
3 |
Correct |
5 ms |
768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
640 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
3 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
990 ms |
22664 KB |
Output is correct |
2 |
Correct |
3253 ms |
58796 KB |
Output is correct |
3 |
Correct |
1118 ms |
33224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
4968 KB |
Output is correct |
2 |
Correct |
807 ms |
51472 KB |
Output is correct |
3 |
Correct |
875 ms |
21016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2204 ms |
59492 KB |
Output is correct |
2 |
Correct |
4139 ms |
108396 KB |
Output is correct |
3 |
Correct |
1135 ms |
35420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
10208 KB |
Output is correct |
2 |
Correct |
2941 ms |
106800 KB |
Output is correct |
3 |
Correct |
1088 ms |
33896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1134 ms |
35340 KB |
Output is correct |
2 |
Execution timed out |
5102 ms |
207704 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1104 ms |
35268 KB |
Output is correct |
2 |
Execution timed out |
5107 ms |
206100 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
97 ms |
5612 KB |
Output is correct |
2 |
Execution timed out |
5112 ms |
205076 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |