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 N 100005
#define M 10000005
using namespace std;
int arr[N];
int par[N];
int nxt[M];
int pos[M];
vector<pair<int,int>> v[M];
int find(int a){
if(a == par[a])return a;
return par[a] = find(par[a]);
}
bool unite(int a,int b){
a = find(a);
b = find(b);
if(a == b)return 0;
par[b] = a;
return 1;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int n;
cin >> n;
int maxi = 0;
for(int i=1;i<=n;i++){
cin >> arr[i];
pos[arr[i]] = i;
maxi = max(maxi,arr[i]);
}
vector<int> nums;
for(int i=maxi;i>=0;i--){
if(pos[i]){
nxt[i] = i;
nums.push_back(i);
}
else nxt[i] = nxt[i+1];
}
for(auto u:nums){
for(int i=u;i<=maxi;i+=u){
int p = nxt[i+(i==u)];
if(p)
v[p%u].push_back({pos[u],pos[p]});
}
}
vector<pair<int,pair<int,int>>> edges;
for(int i=0;i<=maxi;i++){
for(auto u:v[i]){
edges.push_back({i,u});
}
}
for(int i=0;i<n;i++){
par[i] = i;
}
long long ans = 0;
for(auto u:edges){
if(unite(u.second.first,u.second.second))ans+=u.first;
}
cout << ans;
}
# | 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... |