This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include<bits/stdc++.h>
using namespace std;
#define all(v) (v).begin(), (v).end()
#define cst(T) const T&
template<class A, class B> bool umin(A& var, cst(B) val) {
return (val < var) ? (var = val, true) : false;
}
template<class A, class B> bool umax(A& var, cst(B) val) {
return (var < val) ? (var = val, true) : false;
}
typedef long long Int;
typedef long double Real;
const int MOD = 2004010501;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<class X, class Y> Int random(const X& l, const Y& r) {
return uniform_int_distribution<Int>(l,r)(rng);
}
#define DBG(x) cerr << #x << " = " << x << ' ';
#define DBGn(x) cerr << #x << " = " << x << '\n';
const int N = 1e5 + 50;
const int V = 1e7 + 10;
const int INF = 1e9;
int v2pos[V];
class DisjointSet {
private :
int size; int* label = nullptr;
public :
DisjointSet(int size) : size(size) {
if (label != nullptr) delete label;
label = new int[size + 1];
for (int i = 0; i <= size; i++) label[i] = -1;
}
int find_root(int u) {
if (label[u] < 0) return u;
return label[u] = find_root(label[u]);
}
bool merge_set(int u, int v) {
u = find_root(u), v = find_root(v);
if (u == v) return false;
if (-(label[u]) < -(label[v])) swap(u,v);
return label[u] += label[v], label[v] = u, true;
}
bool same_set(int a, int b) {
return find_root(a) == find_root(b);
}
};
struct Edge {
int u,v;
};
vector<Edge> edges[V];
void solve(int testID) {
// DBGn(testID);
int n;
cin >> n;
vector<int> p(n);
for (auto &pi : p) cin >> pi;
sort(all(p));
p.resize(unique(all(p)) - begin(p));
n = int(size(p));
for (int i = 0; i < n; i++) v2pos[p[i]] = i;
set<int> values(all(p));
auto find_edges = [&] (int a, int threshold = 0) {
if (!values.empty()) {
int mq = max(0, threshold / a) + 1;
do {
auto it = values.lower_bound(mq * a);
if (it == end(values)) break;
edges[(*it) % a].push_back({a,*it});
mq = (*it) / a + 1;
} while (1);
}
};
for (auto p_i : p) {
values.erase(p_i);
find_edges(p_i);
}
DisjointSet dsu(n);
Int ans = 0;
for (int w = 0; w < p.back(); w++) {
for (auto [u,v] : edges[w]) {
u = v2pos[u], v = v2pos[v];
if (dsu.merge_set(u,v)) ans += w;
}
}
cout << ans;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define task "WF"
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
int numTests = 1;
// cin >> numTests;
for (int i = 1; i <= numTests; i++) solve(i);
}
Compilation message (stderr)
sirni.cpp: In function 'int main()':
sirni.cpp:110:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
110 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:111:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
111 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |