//author: Ahmet Alp Orakci
#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
const int MAXN = 2E7 + 5;
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;
}
};
pair <int, int> sieve[MAXN];
vector <bool> ok(MAXN);
pair <int, int> pref[MAXN];
pair <int, int> suf[MAXN];
int loc[MAXN];
int ptr;
#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();
if(vec.front() == 1)
return cout << 0, void();
for(int &i : vec)
ok[i] = true, loc[i] = ptr++;
DSU dsu(n);
for(int i = 1; i < MAXN; i++) {
if(ok[i]) {
for(int j = 2 * i; j < MAXN; j += i) {
sieve[j] = {i, loc[i]};
if(ok[j]) {
dsu.unite(loc[i], loc[j]);
}
}
}
}
pref[0] = {-1, -1};
for(int i = 1; i < MAXN; i++) {
if(!sieve[i].first)
pref[i] = pref[i -1];
else
pref[i] = {i, sieve[i].second};
}
suf[MAXN -1] = {-1, -1};
for(int i = MAXN -2; i >= 1; i--) {
if(!sieve[i].first)
suf[i] = suf[i +1];
else
suf[i] = {i, sieve[i].second};
}
vector <tuple <int, int, int>> edges;
i64 ans = 0;
for(int &i : vec) {
if(suf[i].first != -1) {
edges.emplace_back(vec[suf[i].second] - (suf[i].first - i), suf[i].second, loc[i]);
}
if(pref[i].first != -1) {
edges.emplace_back(i - pref[i].first, pref[i].second, loc[i]);
}
}
sort(edges.begin(), edges.end());
for(auto [c, u, v] : edges) {
//cerr << c << " " << u << " " << v << "\n";
if(dsu.unite(u, v)) {
ans += c;
}
}
cout << ans;
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 |
208 ms |
509948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
372 ms |
472652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
212 ms |
510004 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
499 ms |
481384 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
221 ms |
477520 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
661 ms |
481236 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
511 ms |
476964 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
258 ms |
516308 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
262 ms |
516476 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
204 ms |
512848 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |