#include <bits/stdc++.h>
using namespace std;
#ifdef _WIN32
#define getchar_unlocked _getchar_nolock
#endif
inline int readInt()
{
int c = getchar_unlocked(), x = 0;
while (c < '0' or c > '9')
c = getchar_unlocked();
for (; c >= '0' and c <= '9'; c = getchar_unlocked())
x = x * 10 + (c - '0');
return x;
}
struct Edge
{
int u, v, w;
Edge() {}
Edge(int u, int v, int w) : u(u), v(v), w(w) {}
bool inline operator<(const Edge &rhs) const { return w < rhs.w; }
};
const int MAXN = 1e5, MAXP = 1e7;
int n, nxt[MAXP + 5], par[MAXN + 5], cnt = 0;
vector<int> p;
Edge edges[40000007];
int fnd(int u) { return u == par[u] ? u : par[u] = fnd(par[u]); }
bool merge(int u, int v)
{
u = fnd(u), v = fnd(v);
if (u == v)
return false;
par[v] = u;
return true;
}
signed main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL), cout.tie(NULL);
n = readInt();
for (int i = 0, x; i < n; i++)
{
x = readInt();
p.push_back(x);
}
sort(p.begin(), p.end());
p.erase(unique(p.begin(), p.end()), p.end());
n = p.size();
for (int i = 0; i < n - 1; i++)
{
nxt[p[i]] = i;
for (int j = p[i] + 1; j < p[i + 1]; j++)
nxt[j] = i + 1;
}
nxt[p[n - 1]] = n - 1;
for (int i = 0; i < n; i++)
{
edges[cnt++] = Edge(i, nxt[p[i] + 1], p[nxt[p[i] + 1]] % p[i]);
for (int k = p[i] << 1; k <= p[n - 1]; k += p[i])
{
edges[cnt++] = Edge(i, nxt[k], p[nxt[k]] % p[i]);
k = p[nxt[k]] / p[i] * p[i];
}
}
sort(edges, edges + cnt);
for (int i = 0; i < n; i++)
par[i] = i;
int ans = 0;
for (auto &c : edges)
if (merge(c.u, c.v))
ans += c.w;
printf("%d", 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... |