This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#pragma GCC optimze("O3")
#pragma GCC target("avx,avx2,sse4")
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
struct DSU {
vector<int> p, r;
DSU(int n) {
p.assign(n,0);
r.assign(n,0);
for (int i=0;i<n;i++)p[i]=i;
}
int get(int i) {
if (i != p[i]) p[i]=get(p[i]);
return p[i];
}
bool unite(int a,int b) {
a=get(a);
b=get(b);
if (a==b)return false;
if (r[a]<r[b])swap(a,b);
p[b]=a;
if (r[a]==r[b])r[a]++;
return true;
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n;cin>>n;
vector<int> a(n);
for (int &x:a)cin>>x;
sort(a.begin(),a.end());
vector<int> b;
for (int x:a) {
if (b.size()==0||b.back()!=x)b.push_back(x);
}
if (b[0] == 1) {
cout<<0<<"\n";
return 0;
}
vector<bool> cont(b.back()+1, false);
for (int x:b) {
cont[x]=true;
}
vector<int> clos(b.back()+1,-1);
for (int x:b) {
clos[x]=x;
}
for (int i=1;i<clos.size();i++) {
clos[i]=max(clos[i], clos[i-1]);
}
typedef array<int,3> pii;
vector<pii> edg;
for (int x:b) {
vector<pii> tmp;
for (int j=2*x;j<=b.back();j++) {
if (clos[j] != -1) {
if (!tmp.size()) {
tmp.push_back({clos[j] % x, clos[j], x});
}
else if (tmp.back()[1] != clos[j]) {
tmp.push_back({clos[j] % x, clos[j], x});
}
}
}
for (auto x:tmp) edg.push_back(x);
}
sort(edg.begin(),edg.end());
map<int,int> mp;
for (int i=0;i<b.size();i++) {
mp[b[i]]=i;
}
for (auto &x:edg) {
x[1]=mp[x[1]];
x[2]=mp[x[2]];
}
DSU d(b.size());
int cost=0;
for (auto x:edg) {
if (d.unite(x[1], x[2])) {
cost+=x[0];
}
}
cout<<cost<<"\n";
}
Compilation message (stderr)
sirni.cpp:1: warning: ignoring '#pragma GCC optimze' [-Wunknown-pragmas]
1 | #pragma GCC optimze("O3")
|
sirni.cpp: In function 'int main()':
sirni.cpp:61:19: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i=1;i<clos.size();i++) {
| ~^~~~~~~~~~~~
sirni.cpp:86:19: warning: comparison of integer expressions of different signedness: 'int64_t' {aka 'long int'} and 'std::vector<long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
86 | for (int i=0;i<b.size();i++) {
| ~^~~~~~~~~
# | 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... |