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>
using namespace std;
const int NN = 1e5 + 4;
#define _for(i, a, b) for (int i = (a); i < (int)(b); ++i)
int p[NN];
int pa[NN];
int find_set(int x) { return pa[x] == x ? x : pa[x] = find_set(pa[x]); }
struct Edge {
int u, v, w;
bool operator<(const Edge &e) const { return w < e.w; }
};
int main() {
ios::sync_with_stdio(false), cin.tie(0);
vector<Edge> es;
int n;
cin >> n;
_for(i, 0, n) cin >> p[i], pa[i] = i;
// for (int i = 1; i <= n; ++i) cin >> p[i];
// sort(p + 1, p + n + 1);
sort(p, p + n);
// n = unique(p + 1, p + n + 1) - p - 1;
n = unique(p, p + n) - p;
// for (int i = 1; i <= n; ++i) pa[i] = i;
long long ans = 0;
// for (int i = 1; i <= n; ++i)
_for(i, 0, n)
{
int x = p[i];
for (int y = x; y <= p[n-1]; y += x) {
// int k = lower_bound(p + i + 1, p + n + 1, y) - p;
int k = lower_bound(p + i + 1, p + n, y) - p;
// if (k <= n && p[k] / x == y / x)
if (k < n && p[k] / x == y / x)
es.push_back(Edge{i, k, p[k] % p[i]});
}
}
sort(es.begin(), es.end());
for (auto e : es) {
int u = find_set(e.u), v = find_set(e.v);
if (u != v) ans += e.w, pa[u] = v;
}
cout << ans << endl;
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... |