#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e5+10;
const int maxv = 1e7+1;
struct DSU
{
int pai[maxn], peso[maxn];
void init(int n)
{
for (int i = 1; i <= n; i++)
pai[i] = i, peso[i] = 1;
}
int Find(int x)
{
if (pai[x] == x) return x;
return pai[x] = Find(pai[x]);
}
void Join(int x, int y)
{
x = Find(x), y = Find(y);
if (x == y) return;
if (peso[x] < peso[y]) swap(x, y);
pai[y] = x, peso[x] += peso[y];
}
} dsu;
int a[maxn];
int prim[maxv];
int main(void)
{
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%d", &a[i]);
sort(a+1, a+n+1);
int ptr = 2;
for (int i = a[1]; i <= a[n]; i++)
{
while (ptr <= n && a[ptr] < i)
ptr++;
prim[i] = ptr;
}
int m = 0;
dsu.init(n);
vector<pair<int, pii>> V;
for (int i = 2; i <= n; i++)
V.push_back({a[i]-a[i-1], {i-1, i}});
for (int i = 1; i <= n; i++)
{
int j;
for (j = i; j <= n && a[i] == a[j]; j++)
if (j != i)
dsu.Join(i, j), m++;
i = j-1;
for (int v = 2*a[i]; v <= a[n]; v += a[i])
V.push_back({a[prim[v]]-v, {i, prim[v]}});
}
sort(V.begin(), V.end());
ll ans = 0;
for (auto pp: V)
{
if (m == n-1) break;
int u = pp.ss.ff, v = pp.ss.ss, w = pp.ff;
if (dsu.Find(u) != dsu.Find(v))
{
ans += 1ll*w;
dsu.Join(u, v), ++m;
}
}
printf("%lld\n", ans);
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:47:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
47 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
sirni.cpp:50:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
50 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 ms |
39420 KB |
Output is correct |
2 |
Correct |
500 ms |
88764 KB |
Output is correct |
3 |
Correct |
47 ms |
39848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
512 KB |
Output is correct |
2 |
Runtime error |
1059 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
39544 KB |
Output is correct |
2 |
Correct |
34 ms |
39416 KB |
Output is correct |
3 |
Correct |
35 ms |
39800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
220 ms |
30164 KB |
Output is correct |
2 |
Correct |
792 ms |
54828 KB |
Output is correct |
3 |
Correct |
418 ms |
54828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
7668 KB |
Output is correct |
2 |
Correct |
422 ms |
54720 KB |
Output is correct |
3 |
Correct |
220 ms |
28128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
471 ms |
54840 KB |
Output is correct |
2 |
Correct |
1126 ms |
104056 KB |
Output is correct |
3 |
Correct |
374 ms |
30148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
7920 KB |
Output is correct |
2 |
Correct |
1174 ms |
104060 KB |
Output is correct |
3 |
Correct |
388 ms |
30172 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
333 ms |
65356 KB |
Output is correct |
2 |
Runtime error |
1209 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
414 ms |
65356 KB |
Output is correct |
2 |
Runtime error |
1219 ms |
786436 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
42860 KB |
Output is correct |
2 |
Runtime error |
1675 ms |
786432 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
3 |
Halted |
0 ms |
0 KB |
- |