//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
struct DSU {
int n;
vector <int> par;
DSU(int n) : n(n) {
par.resize(n);
iota(par.begin(), par.end(), 0);
}
inline int get(int a) {
return par[a] = (par[a] == a ? a : get(par[a]));
}
bool same(int u, int v) {
return get(u) == get(v);
}
bool unite(int u, int v) {
if(same(u, v))
return false;
par[get(u)] = get(v);
return true;
}
};
#define ONLINE_JUDGE
void solve() {
int n;
cin >> n;
vector <int> vec(n);
for(int &i : vec)
cin >> i;
priority_queue <tuple <int, int, int>, vector <tuple <int, int, int>>, greater <tuple <int, int, int>>> pq;
for(int i = 0; i < int(5E6); i++) {
int a = rand() % n, b = rand() % n;
pq.emplace(min(vec[a] % vec[b], vec[b] % vec[a]), a, b);
}
i64 res = 0;
DSU dsu(n);
while(!pq.empty()) {
auto [c, u, v] = pq.top();
pq.pop();
if(dsu.unite(u, v)) {
res += c;
}
}
cout << res;
return;
}
signed main() {
#ifndef ONLINE_JUDGE
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL); cout.tie(NULL);
int t = 1; //cin >> t;
for(int i = 1; i <= t; i++) {
solve();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1578 ms |
100556 KB |
Output is correct |
2 |
Correct |
1596 ms |
99480 KB |
Output is correct |
3 |
Correct |
1567 ms |
99504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1640 ms |
99916 KB |
Output is correct |
2 |
Correct |
1600 ms |
98976 KB |
Output is correct |
3 |
Correct |
1598 ms |
101016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1580 ms |
99232 KB |
Output is correct |
2 |
Correct |
1598 ms |
100100 KB |
Output is correct |
3 |
Correct |
1554 ms |
99648 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1615 ms |
99676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1567 ms |
100000 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1599 ms |
99932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1682 ms |
100232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1724 ms |
100852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1611 ms |
101116 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1618 ms |
100008 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |