#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 1e7 + 5;
vector <pair <int, int>> edge[N];
int a[N];
int p[N];
int par[N];
int val[N];
int n;
bool mini(int &a, int b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int getpar(int u) {
return (par[u] < 0) ? u : (par[u] = getpar(par[u]));
}
bool join(int u, int v) {
u = getpar(u);
v = getpar(v);
if (u == v)
return false;
if (par[u] > par[v])
swap(u, v);
par[u] += par[v];
par[v] = u;
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++)
cin >> a[i];
sort(a + 1, a + n + 1);
n = unique(a + 1, a + n + 1) - (a + 1);
for (int i = 1; i <= n; i++) {
p[a[i]] = a[i];
val[a[i]] = 1e9;
}
int mx = a[n];
for (int i = mx - 1; i >= 1; i--)
if (!p[i])
p[i] = p[i + 1];
for (int i = 1; i <= n; i++) {
int x = a[i];
if (i < n)
edge[p[x + 1] - x].push_back(mp(x, p[x + 1]));
for (int j = 2 * x; j <= mx; j += x) {
// cout << p[j] << " " << j << "\n";
edge[p[j] - j].push_back(mp(x, p[j]));
}
}
for (int i = 1; i <= mx; i++)
par[i] = -1;
long long ans = 0;
for (int i = 0; i <= mx; i++)
for (pair <int, int> e : edge[i])
ans += i * join(e.fi, e.se);
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
221 ms |
317148 KB |
Output is correct |
2 |
Correct |
496 ms |
347732 KB |
Output is correct |
3 |
Correct |
231 ms |
317488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
235400 KB |
Output is correct |
2 |
Runtime error |
2960 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
317472 KB |
Output is correct |
2 |
Correct |
272 ms |
315756 KB |
Output is correct |
3 |
Correct |
247 ms |
317572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
257492 KB |
Output is correct |
2 |
Correct |
359 ms |
286856 KB |
Output is correct |
3 |
Correct |
255 ms |
269248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
249088 KB |
Output is correct |
2 |
Correct |
318 ms |
270692 KB |
Output is correct |
3 |
Correct |
210 ms |
251860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
269540 KB |
Output is correct |
2 |
Correct |
451 ms |
301652 KB |
Output is correct |
3 |
Correct |
247 ms |
265876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
241164 KB |
Output is correct |
2 |
Correct |
469 ms |
307248 KB |
Output is correct |
3 |
Correct |
248 ms |
268444 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
449 ms |
367020 KB |
Output is correct |
2 |
Correct |
2975 ms |
712148 KB |
Output is correct |
3 |
Correct |
492 ms |
369996 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
462 ms |
371376 KB |
Output is correct |
2 |
Runtime error |
3087 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
286 ms |
350016 KB |
Output is correct |
2 |
Correct |
4779 ms |
727824 KB |
Output is correct |
3 |
Correct |
265 ms |
268672 KB |
Output is correct |