// #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);
}
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+=x) {
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});
}
}
}
if (tmp.size() == 0 || tmp.back()[1] != clos[b.back()]) {
tmp.push_back({clos[b.back()] % x, clos[b.back()], 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])) {
// cout<<b[x[1]]<<" "<<b[x[2]]<<"\n";
cost+=x[0];
}
}
cout<<cost<<"\n";
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:51: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]
51 | for (int i=1;i<clos.size();i++) {
| ~^~~~~~~~~~~~
sirni.cpp:80: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]
80 | for (int i=0;i<b.size();i++) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
47 ms |
78932 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
860 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
42 ms |
78936 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
404 ms |
42092 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
54 ms |
15068 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
888 ms |
110588 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
118 ms |
15888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
609 ms |
134028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
634 ms |
134748 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
112 ms |
86096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |