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>
#include <functional>
#include <numeric>
using namespace std;
int main(){
int N;
cin >> N;
set<int> s;
int a;
for(int i = 0; i < N; ++i){
cin >> a;
s.insert(-a);
}
N = s.size();
vector<int> v;
for(auto x: s)v.emplace_back(-x);
int m = 1e7 + 2;
vector<vector<int>> p(m);
for(int j = 0; j < N; ++j){
for(int i = v[j]; i < m; i += v[j]){
p[i].push_back(j);
}
}
int c = v[0];
long long ans = 0;
vector<vector<pair<int,int>>> adj(N);
vector<array<int,3>> eg;
for(int i = 0; i < N; ++i){
auto x = v[i];
if(c > x)--c;
while(c > x){
--c;
for(auto j: p[c]){
eg.push_back({v[i-1] -c, j, i-1});
}
}
if(c == x && p[x].size() == 1)--c;
while(c > 0 && p[c].empty())--c;
for(auto j: p[c]){
eg.push_back({x-c, i, j});
}
}
vector<int> dad(N), sz(N, 0);
iota(dad.begin(), dad.end(), 0);
function<int(int)> find=[&](int a){
if(dad[a] != a)dad[a] = find(dad[a]);
return dad[a];
};
auto uni = [&](int a, int b) -> bool{
a = find(a);
b = find(b);
if(a == b)return 0;
if(sz[a] < sz[b])swap(a,b);
dad[b] = a;
sz[a] += sz[b];
return 1;
};
sort(eg.begin(), eg.end());
for(auto [w, i, j]: eg){
if(uni(i, j))ans += w;
}
cout << ans << endl;
return 0;
}
# | 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... |