#include <bits/stdc++.h>
// author : aykhn
using namespace std;
typedef long long ll;
#define pb push_back
#define ins insert
#define mpr make_pair
#define all(v) v.begin(), v.end()
#define bpc __builtin_popcountll
#define pii pair<ll, ll>
#define pll pair<ll, ll>
#define fi first
#define se second
#define int ll
#define infll 0x3F3F3F3F3F3F3F3F
#define inf 0x3F3F3F3F
struct DSU
{
vector<int> e;
void init(int n)
{
e.assign(n + 1, -1);
}
int get(int x)
{
if (e[x] < 0) return x;
return e[x] = get(e[x]);
}
int tog(int x, int y)
{
return get(x) == get(y);
}
int unite(int x, int y)
{
x = get(x);
y = get(y);
if (x == y) return 0;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return 1;
}
};
const int MXN = 5e5 + 5;
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(nullptr);
int n;
cin >> n;
int a[n + 1];
a[0] = 0;
for (int i = 1; i <= n; i++) cin >> a[i];
sort(a + 1, a + n + 1);
set<array<int, 3>> e;
for (int i = 1; i <= n; i++)
{
if (a[i] == a[i - 1]) continue;
for (int j = a[i]; j <= a[n]; j += a[i])
{
int ind = (j == a[i] ? upper_bound(a + 1, a + n + 1, j) - a : lower_bound(a + 1, a + n + 1, j) - a);
if (ind > n) break;
e.ins({a[ind] % a[i], i, ind});
}
}
int res = 0;
DSU dsu;
dsu.init(n);
for (array<int, 3> a : e)
{
if (dsu.unite(a[1], a[2])) res += a[0];
}
cout << res << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
281 ms |
12320 KB |
Output is correct |
3 |
Correct |
5 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
860 KB |
Output is correct |
2 |
Correct |
3273 ms |
4416 KB |
Output is correct |
3 |
Correct |
6 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
3 ms |
860 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
588 ms |
65356 KB |
Output is correct |
2 |
Correct |
2444 ms |
243684 KB |
Output is correct |
3 |
Correct |
928 ms |
93832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
9300 KB |
Output is correct |
2 |
Correct |
2179 ms |
134388 KB |
Output is correct |
3 |
Correct |
576 ms |
66896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1443 ms |
146148 KB |
Output is correct |
2 |
Correct |
3206 ms |
312308 KB |
Output is correct |
3 |
Correct |
826 ms |
86716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
23056 KB |
Output is correct |
2 |
Correct |
3733 ms |
297144 KB |
Output is correct |
3 |
Correct |
811 ms |
82516 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
883 ms |
77648 KB |
Output is correct |
2 |
Execution timed out |
5080 ms |
368400 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
958 ms |
76720 KB |
Output is correct |
2 |
Execution timed out |
5090 ms |
283824 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
12116 KB |
Output is correct |
2 |
Execution timed out |
5075 ms |
347128 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |