//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() % 1000 == 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 |
71 ms |
7880 KB |
Output is correct |
2 |
Correct |
86 ms |
8392 KB |
Output is correct |
3 |
Correct |
83 ms |
7628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
86 ms |
8392 KB |
Output is correct |
2 |
Correct |
91 ms |
7884 KB |
Output is correct |
3 |
Correct |
86 ms |
8392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
88 ms |
6856 KB |
Output is correct |
2 |
Correct |
67 ms |
7264 KB |
Output is correct |
3 |
Correct |
85 ms |
7116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5067 ms |
13608 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
710 ms |
2496 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5097 ms |
13852 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5075 ms |
14552 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5007 ms |
14144 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
5062 ms |
15280 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1411 ms |
3592 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |