#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 idx[(int)1e7+10];
int number[(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);
}
int ctr = 0;
for (int num : nums) {
idx[num] = ctr++;
}
for (int i = 0; i < n; i++) {
sz[i] = 1;
pa[i] = i;
}
auto it = nums.begin();
for (int i = 0; i <= 1e7; i++) {
while (it != nums.end() && *it < i) it++;
if (it == nums.end()) number[i] = -1;
else number[i] = *it;
}
for (int num : nums) {
int prev = -1;
for (int i = num; i <= 1e7; i += num) {
int it = i == num ? number[i+1] : number[i];
if (it != -1) {
if (prev == it) continue;
prev = it;
edges[it%num].emplace_back(idx[num], idx[it]);
} 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
205 ms |
278264 KB |
Output is correct |
2 |
Correct |
257 ms |
278776 KB |
Output is correct |
3 |
Correct |
207 ms |
278520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
208 ms |
274552 KB |
Output is correct |
2 |
Correct |
526 ms |
275704 KB |
Output is correct |
3 |
Correct |
211 ms |
278520 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
204 ms |
278520 KB |
Output is correct |
2 |
Correct |
209 ms |
276984 KB |
Output is correct |
3 |
Correct |
207 ms |
278476 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
292852 KB |
Output is correct |
2 |
Correct |
461 ms |
319540 KB |
Output is correct |
3 |
Correct |
335 ms |
297316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
227 ms |
280568 KB |
Output is correct |
2 |
Correct |
346 ms |
300408 KB |
Output is correct |
3 |
Correct |
303 ms |
290104 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
397 ms |
305116 KB |
Output is correct |
2 |
Correct |
504 ms |
334592 KB |
Output is correct |
3 |
Correct |
351 ms |
296024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
280284 KB |
Output is correct |
2 |
Correct |
476 ms |
328144 KB |
Output is correct |
3 |
Correct |
339 ms |
295716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
402 ms |
330872 KB |
Output is correct |
2 |
Correct |
1698 ms |
526324 KB |
Output is correct |
3 |
Correct |
420 ms |
334200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
405 ms |
330732 KB |
Output is correct |
2 |
Correct |
2409 ms |
616136 KB |
Output is correct |
3 |
Correct |
452 ms |
337772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
251 ms |
311688 KB |
Output is correct |
2 |
Correct |
2361 ms |
604008 KB |
Output is correct |
3 |
Correct |
347 ms |
298084 KB |
Output is correct |