#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;
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 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 m = 0;
dsu.init(n);
vector<pii> V;
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 = a[i]; v <= a[n]; v += a[i])
V.push_back({v, i});
}
sort(V.begin(), V.end());
set<pair<int, pii>> st;
for (int i = 2; i <= n; i++)
st.insert({a[i]-a[i-1], {i-1, i}});
int ptr = (int)V.size()-1;
for (int i = n; i >= 1; i--)
{
while (ptr >= 0 && V[ptr].ff > a[i])
ptr--;
while (ptr >= 0 && V[ptr].ff > a[i-1])
{
st.insert({a[i]-V[ptr].ff, {i, V[ptr].ss}});
ptr--;
}
}
ll ans = 0;
for (auto pp: st)
{
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:45:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
45 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
sirni.cpp:48:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
48 | scanf("%d", &a[i]);
| ~~~~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
768 KB |
Output is correct |
2 |
Correct |
2423 ms |
195608 KB |
Output is correct |
3 |
Correct |
9 ms |
2044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
896 KB |
Output is correct |
2 |
Execution timed out |
5090 ms |
525900 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
768 KB |
Output is correct |
2 |
Correct |
2 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
1152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
667 ms |
82112 KB |
Output is correct |
2 |
Correct |
2205 ms |
302104 KB |
Output is correct |
3 |
Correct |
1216 ms |
160792 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
14180 KB |
Output is correct |
2 |
Correct |
1339 ms |
165912 KB |
Output is correct |
3 |
Correct |
643 ms |
83132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1323 ms |
171468 KB |
Output is correct |
2 |
Correct |
2981 ms |
421160 KB |
Output is correct |
3 |
Correct |
1059 ms |
143676 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
29208 KB |
Output is correct |
2 |
Correct |
3233 ms |
438228 KB |
Output is correct |
3 |
Correct |
1137 ms |
151168 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1174 ms |
102012 KB |
Output is correct |
2 |
Execution timed out |
5066 ms |
527052 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1602 ms |
138428 KB |
Output is correct |
2 |
Execution timed out |
5125 ms |
527632 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
14948 KB |
Output is correct |
2 |
Execution timed out |
5042 ms |
526920 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |