#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];
vector<ii> edges[(int)1e7+10];
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;
}
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[*it%num].emplace_back(idx[num], idx[*it]);
if (*it - i < num) assert(*it - i == *it % num);
} else {
break;
}
}
/*
for (int num2 : nums) {
if (num < num2)
edges.pb(mp(num2%num, mp(idx[num], idx[num2])));
}
*/
}
ll ans = 0;
for (int c = 0; c <= 1e7; c++) {
for (auto e : edges[c]) {
if (findSet(e.f) != findSet(e.s)) {
ans += c;
unite(e.f, e.s);
}
}
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
235504 KB |
Output is correct |
2 |
Correct |
232 ms |
237692 KB |
Output is correct |
3 |
Correct |
162 ms |
235512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
235312 KB |
Output is correct |
2 |
Correct |
717 ms |
236024 KB |
Output is correct |
3 |
Correct |
163 ms |
235512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
235384 KB |
Output is correct |
2 |
Correct |
159 ms |
235256 KB |
Output is correct |
3 |
Correct |
162 ms |
235512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
964 ms |
254344 KB |
Output is correct |
2 |
Correct |
2501 ms |
280856 KB |
Output is correct |
3 |
Correct |
987 ms |
257888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
238200 KB |
Output is correct |
2 |
Correct |
675 ms |
258552 KB |
Output is correct |
3 |
Correct |
824 ms |
253024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1893 ms |
266668 KB |
Output is correct |
2 |
Correct |
3220 ms |
296116 KB |
Output is correct |
3 |
Correct |
1054 ms |
257660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
317 ms |
241884 KB |
Output is correct |
2 |
Correct |
2329 ms |
289324 KB |
Output is correct |
3 |
Correct |
928 ms |
256616 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1077 ms |
257388 KB |
Output is correct |
2 |
Execution timed out |
5085 ms |
354780 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1060 ms |
257224 KB |
Output is correct |
2 |
Execution timed out |
5091 ms |
359256 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
240 ms |
239364 KB |
Output is correct |
2 |
Execution timed out |
5091 ms |
378824 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |