//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 * 2; k < MAXN; k += i) {
int itt = upper_bound(vec.begin(), vec.end(), k -1) - 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 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
242 ms |
50600 KB |
Output is correct |
3 |
Correct |
3 ms |
852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
508 KB |
Output is correct |
2 |
Runtime error |
844 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
516 ms |
27228 KB |
Output is correct |
2 |
Correct |
2004 ms |
52092 KB |
Output is correct |
3 |
Correct |
968 ms |
51228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
3780 KB |
Output is correct |
2 |
Correct |
854 ms |
50872 KB |
Output is correct |
3 |
Correct |
844 ms |
26976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1140 ms |
52252 KB |
Output is correct |
2 |
Correct |
2741 ms |
100236 KB |
Output is correct |
3 |
Correct |
860 ms |
26548 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
591 ms |
8160 KB |
Output is correct |
2 |
Correct |
2757 ms |
101192 KB |
Output is correct |
3 |
Correct |
891 ms |
27472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
27820 KB |
Output is correct |
2 |
Runtime error |
1983 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
334 ms |
28084 KB |
Output is correct |
2 |
Runtime error |
1837 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
4044 KB |
Output is correct |
2 |
Runtime error |
2505 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |