This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define fi first
#define se second
#define mp make_pair
using namespace std;
const int N = 1e7 + 5;
vector <pair <int, int>> edge[N];
int a[N];
int p[N];
int par[N];
int n;
bool mini(int &a, int b) {
if (a > b) {
a = b;
return true;
}
return false;
}
int getpar(int u) {
return (par[u] < 0) ? u : (par[u] = getpar(par[u]));
}
bool join(int u, int v) {
u = getpar(u);
v = getpar(v);
if (u == v)
return false;
if (par[u] > par[v])
swap(u, v);
par[u] += par[v];
par[v] = u;
return true;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
int mx = 0;
for (int i = 1; i <= n; i++) {
int x; cin >> x;
p[x] = x;
mx = max(mx, x);
}
for (int i = mx - 1; i >= 1; i--)
if (!p[i])
p[i] = p[i + 1];
for (int i = 1; i <= mx; i++) if (p[i] == i) {
if (i < mx)
edge[p[i + 1] - i].push_back(mp(i, p[i + 1]));
for (int j = 2 * i; j <= mx; j += i) {
// cout << p[j] << " " << j << "\n";
edge[p[j] - j].push_back(mp(i, p[j]));
}
}
for (int i = 1; i <= mx; i++)
par[i] = -1;
long long ans = 0;
for (int i = 0; i <= mx; i++)
for (pair <int, int> e : edge[i])
ans += i * join(e.fi, e.se);
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |