#include <bits/stdc++.h>
using namespace std;
using namespace std::placeholders;
// #define rand __rand
// mt19937 rand(chrono::system_clock::now().time_since_epoch().count()); // or mt19937_64
#define llong long long
#define xx first
#define yy second
#define len(x) ((int)x.size())
#define rep(i,n) for (int i = -1; ++ i < n; )
#define rep1(i,n) for (int i = 0; i ++ < n; )
#define all(x) x.begin(), x.end()
template<class T>
using min_priority_queue = priority_queue<T, vector<T>, greater<T>>;
#define maxn 101010
#define maxval ((llong)1e7+10)
int n;
int a[maxn];
int lower_b[maxval] = {0};
int dsu[maxn];
int n_set;
int findp(int u) { return u == dsu[u] ? u : dsu[u] = findp(dsu[u]); }
void join(int u, int v) {
u = findp(u);
v = findp(v);
if (u == v) return ;
if (rand() & 1) swap(u, v);
dsu[u] = v;
--n_set;
}
int main(void) {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n;
rep(i, n) cin >> a[i];
sort(a, a + n);
n = (int)(unique(a, a + n) - a);
int f = 0;
rep(i, n) {
for (; f <= a[i]; ++f) lower_b[f] = i;
}
min_priority_queue<tuple<int, int, int>> pq;
rep(i, n) {
for (int k = 1; k * a[i] <= a[n - 1]; ++k) {
int t = lower_b[k * a[i]];
if (t != n and a[t] == a[i]) ++t;
if (t == n) break;
pq.push({a[t] % a[i], i, t});
k = a[t] / a[i];
}
}
// clog << "nice" << endl;
n_set = n;
rep1(i, n) dsu[i] = i;
llong ans = 0;
while (n_set != 1) {
int cost, src, cur;
tie(cost, src, cur) = pq.top();
pq.pop();
if (findp(src) != findp(cur)) {
ans += cost;
join(src, cur);
}
int div = a[cur] / a[src];
++cur;
if (cur == n or a[cur] / a[src] != div) continue;
pq.push({a[cur] % a[src], src, cur});
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
39672 KB |
Output is correct |
2 |
Correct |
59 ms |
42600 KB |
Output is correct |
3 |
Correct |
39 ms |
39736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
632 KB |
Output is correct |
2 |
Correct |
41 ms |
40320 KB |
Output is correct |
3 |
Correct |
50 ms |
39800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
39600 KB |
Output is correct |
2 |
Correct |
43 ms |
39544 KB |
Output is correct |
3 |
Correct |
38 ms |
39672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
452 ms |
17380 KB |
Output is correct |
2 |
Correct |
1237 ms |
54340 KB |
Output is correct |
3 |
Correct |
233 ms |
29680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
39 ms |
7656 KB |
Output is correct |
2 |
Correct |
335 ms |
54236 KB |
Output is correct |
3 |
Correct |
424 ms |
15560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
932 ms |
54392 KB |
Output is correct |
2 |
Correct |
859 ms |
103544 KB |
Output is correct |
3 |
Correct |
236 ms |
29664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
7776 KB |
Output is correct |
2 |
Correct |
840 ms |
103568 KB |
Output is correct |
3 |
Correct |
217 ms |
29788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
643 ms |
64944 KB |
Output is correct |
2 |
Correct |
4256 ms |
434152 KB |
Output is correct |
3 |
Correct |
660 ms |
64716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
381 ms |
64800 KB |
Output is correct |
2 |
Execution timed out |
5033 ms |
434216 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
130 ms |
42988 KB |
Output is correct |
2 |
Execution timed out |
5097 ms |
434224 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |