#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")
#pragma GCC optimize("Ofast")
#define dbg(x) cerr << #x << " = " << x << "\n"
#define fast_io ios_base::sync_with_stdio(0); cin.tie(0); cout.tie()
using namespace std;
using ll = long long;
const int oo = int(1e7) + 1;
const int N = int(1e5) + 10;
int n, a[N], pr[N];
vector <pair <int, int> > g[oo];
int f(int x) { return (x == pr[x]) ? x : pr[x] = f(pr[x]); }
int main() {
fast_io;
cin >> n;
for (int i = 0; i < n; ++i)
cin >> a[i], pr[i] = i;
sort(a, a + n);
for (int i = 0; i < n - 1; ++i) {
if (a[i] == a[i + 1])
g[0].push_back(make_pair(i, i + 1));
for (int j = a[i]; j <= oo; j += a[i]) {
int p = lower_bound(a, a + n, (j == a[i] ? j + 1 : j)) - a;
if (p == n)
break;
while (j <= a[p])
j += a[i];
j -= a[i];
g[a[p] - j].push_back(make_pair(i, p));
}
}
int ans = 0, kol = 0;
for (int i = 0; i < oo; ++i)
for (auto j : g[i]) {
int x = f(j.first);
int y = f(j.second);
if (x == y)
continue;
ans += i;
++kol;
if (kol == n - 1)
return void(cout << ans << "\n"), 0;
pr[y] = x;
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
235384 KB |
Output is correct |
2 |
Correct |
140 ms |
238200 KB |
Output is correct |
3 |
Correct |
132 ms |
235512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
235280 KB |
Output is correct |
2 |
Correct |
173 ms |
236280 KB |
Output is correct |
3 |
Correct |
131 ms |
235384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
235384 KB |
Output is correct |
2 |
Correct |
119 ms |
235256 KB |
Output is correct |
3 |
Correct |
121 ms |
235384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
263 ms |
245500 KB |
Output is correct |
2 |
Correct |
650 ms |
282364 KB |
Output is correct |
3 |
Correct |
349 ms |
255900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
236792 KB |
Output is correct |
2 |
Correct |
1788 ms |
504332 KB |
Output is correct |
3 |
Correct |
291 ms |
247468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
437 ms |
259972 KB |
Output is correct |
2 |
Correct |
924 ms |
313244 KB |
Output is correct |
3 |
Correct |
286 ms |
250200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
166 ms |
239444 KB |
Output is correct |
2 |
Correct |
793 ms |
303912 KB |
Output is correct |
3 |
Correct |
310 ms |
253796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
273 ms |
247928 KB |
Output is correct |
2 |
Correct |
2771 ms |
533360 KB |
Output is correct |
3 |
Correct |
303 ms |
250600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
279 ms |
247884 KB |
Output is correct |
2 |
Correct |
3989 ms |
647600 KB |
Output is correct |
3 |
Correct |
346 ms |
257292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
237560 KB |
Output is correct |
2 |
Correct |
4043 ms |
681320 KB |
Output is correct |
3 |
Correct |
335 ms |
256212 KB |
Output is correct |