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;
#pragma GCC optimize(2)
int nxt[10001000], p[100100],pa[100100],n,mx,ans;
set<tuple<int,int,int>> edges;
int abp(int n) {
return (pa[n]?pa[n]=abp(pa[n]):n);
}
bool un(int a, int b) {
a=abp(a);
b=abp(b);
if(a==b)
return 0;
pa[a]=b;
return 1;
}
int main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> p[i], mx=max(mx,p[i]),nxt[p[i]]=i;
for(int i = mx; i--;)
if(!nxt[i])
nxt[i]=nxt[i+1];
for(int i = 1; i <= n; i++)
for(int j = 2*p[i]; j <= mx; j++)
if(nxt[j])
edges.insert({p[nxt[j]]%p[i],i,nxt[j]});
else break;
for(auto [w,u,v]: edges) {
if(w>mx/2)
break;
if(un(u,v))
ans+=w;
}
cout << ans << '\n';
}
# | 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... |