//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 < n; i++)
for(int j = i +1; j < n; j++)
if(n <= int(1E3) || rand() % 10000 == 0)
pq.emplace(min(vec[i] % vec[j], vec[j] % vec[i]), i, j);
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 |
73 ms |
7628 KB |
Output is correct |
2 |
Correct |
85 ms |
8140 KB |
Output is correct |
3 |
Correct |
83 ms |
6856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
8136 KB |
Output is correct |
2 |
Correct |
88 ms |
8136 KB |
Output is correct |
3 |
Correct |
87 ms |
7432 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
8652 KB |
Output is correct |
2 |
Correct |
67 ms |
8652 KB |
Output is correct |
3 |
Correct |
91 ms |
7372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5051 ms |
2780 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
711 ms |
744 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5032 ms |
2456 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5033 ms |
2256 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5004 ms |
2432 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5040 ms |
2600 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1395 ms |
1364 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |