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;
#define task "test"
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FORD(i, a, b) for(int i = (a); i >= (b); --i)
#define sz(a) (int)(a).size()
#define all(a) (a).begin(), (a).end()
#define bit(s, i) (((s) >> (i)) & 1)
#define ii pair <int, int>
#define fi first
#define se second
#define ll long long
#define eb emplace_back
#define pb push_back
#define __builtin_popcount __builtin_popcountll
void solve();
int32_t main() {
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
solve();
}
const int N = 1e5+5;
int n;
vector <int> p;
vector <pair <ii, int>> edge;
struct DSU {
int par[N];
void init(int n) {
iota(par, par+n, 0);
}
int find(int a) {
return par[a] == a ? a : par[a] = find(par[a]);
}
void unite(int a, int b) {
a = find(a); b = find(b);
if(a != b) {
par[b] = a;
}
}
} dsu;
void solve() {
cin >> n;
p.resize(n);
for(int &i:p) cin >> i;
sort(all(p));
p.erase(unique(all(p)), p.end());
FOR(i, 0, sz(p)-1) {
int it = i + 1;
while(it < sz(p)) {
edge.pb({{i, it}, p[it] % p[i]});
int valnxt = (p[it]/p[i] + 1) * p[i];
it = lower_bound(all(p), valnxt) - p.begin();
}
}
FOR(i, 0, sz(p)-2) {
edge.pb({{i, i+1}, p[i+1]%p[i]});
}
sort(all(edge), [] (pair <ii, int> x, pair <ii, int> y) {return x.se < y.se;});
dsu.init(sz(p));
ll ans = 0;
for(pair <ii, int> e:edge) {
if(dsu.find(e.fi.fi) != dsu.find(e.fi.se)) {
ans = ans + e.se;
dsu.unite(e.fi.fi, e.fi.se);
}
}
cout << ans << '\n';
}
Compilation message (stderr)
sirni.cpp: In function 'int32_t main()':
sirni.cpp:23:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
23 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
sirni.cpp:24:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
24 | 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... |