//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 2E7 + 5;
ostream& operator<< (ostream& os, pair <int, int> &pii) {
return os << "{" << pii.first << " " << pii.second << "}";
}
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 x) {
return par[x] = (x == par[x] ? x : get(par[x]));
}
bool same(int a, int b) {
return get(a) == get(b);
}
bool unite(int u, int v) {
if(same(u, v))
return false;
u = get(u);
v = get(v);
par[v] = u;
return true;
}
};
#define ONLINE_JUDGE
void solve() {
int n;
cin >> n;
vector <int> vec(n);
for(int &i : vec)
cin >> i;
sort(vec.begin(), vec.end());
vec.erase(unique(vec.begin(), vec.end()), vec.end());
n = vec.size();
vector <tuple <int, int, int>> edges;
for(int it = 0; it < n; it++) {
if(it != 0) {
edges.emplace_back(vec[it] % vec[it -1], it -1, it);
}
int i = vec[it];
for(int k = i; k < MAXN; k += i) {
int itt = upper_bound(vec.begin(), vec.end(), k) - vec.begin();
if(itt != n)
edges.emplace_back(vec[itt] % vec[it], it, itt);
}
}
sort(edges.begin(), edges.end());
DSU dsu(n);
i64 ans = 0;
for(auto [c, u, v] : edges) {
//cerr << c << " " << u << " " << v << "\n";
if(dsu.unite(u, v)) {
ans += c;
}
}
bool check = true;
for(int i = 0; i < n; i++)
check &= dsu.same(0, i);
cout << ans;
assert(check);
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 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
132 ms |
536 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
532 ms |
26576 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
75 ms |
5056 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1153 ms |
51260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
594 ms |
7124 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
308 ms |
27104 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
353 ms |
26032 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
3532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |