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
#define pb push_back
#define eb emplace_back
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
const int maxn = 1e5 + 5, inf = 1e9;
int n, a[maxn];
vector<ar<int, 3>> edge;
int lab[maxn];
int finds(int u)
{
if(lab[u] < 0) return u;
return lab[u] = finds(lab[u]);
}
bool merges(int u, int v)
{
u = finds(u); v = finds(v);
if(u == v) return false;
if(lab[v] < lab[u]) swap(u, v);
lab[u] += lab[v]; lab[v] = u;
return true;
}
signed main(void)
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#ifdef LOCAL
freopen("A.INP", "r", stdin);
freopen("A.OUT", "w", stdout);
#endif // LOCAL
cin >> n;
fill(lab + 1, lab + 1 + n, -1);
set<int> se;
for(int i = 1; i <= n; ++i)
cin >> a[i], se.insert(a[i]);
int n = 0;
for(int i : se){
a[++n] = i;
}
for(int i = 1; i <= n; ++i){
if(i < n)
edge.pb({a[i + 1] % a[i], i, i + 1});
int j = i + 1;
for(int num = 2 * a[i]; num <= a[n]; num += a[i]){
while(a[j] < num) ++j;
edge.pb({a[j] % a[i], j, i});
}
}
ll mst = 0;
sort(edge.begin(), edge.end());
for(auto & all : edge){
if(merges(all[1], all[2]))
mst += all[0];
}
cout << mst;
}
# | 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... |