#include <bits/stdc++.h>
//#include <iostream>
//#include <vector>
#define s second
#define pb push_back
#define f first
using namespace std;
const int N = 10000005;
int p[N],m[N],sz[N],a[N],ans,MR[N];
vector <pair<int,pair<int,int> > > edges;
vector <pair<int,int> > d;
int P(int x){
if (x == p[x]) return x;
return p[x] = P(p[x]);
}
void dsu(int a,int b,int c){
int pa = P(a),pb = P(b);
if (pa == pb) return;
ans += c;
if (sz[pa] < sz[pb]) swap(pa,pb);
sz[pa] += sz[pb];
sz[pb] = 0;
p[pb] = pa;
}
int main() {
ios_base::sync_with_stdio(0),cin.tie(NULL),cout.tie(NULL);
int n;
cin>>n;
for (int i = 1; i <= n; i++)
cin>>a[i],p[i] = i,sz[i] = 1,d.pb({a[i],i});
sort(d.begin(),d.end());
int r = d.size() - 1;
for (int i = 10000000; i >= 1; i--){
m[i] = m[i + 1];
while (r >= 0 && i <= d[r].f)
m[i] = d[r--].s;
}
for(int i = n; i >= 1; i--){
if (i < n && d[i].f != d[i + 1].f) MR[i] = d[i + 1].s;
else MR[i] = MR[i + 1];
}
for (int i = 1; i <= n; i++){
if (MR[a[i]] && a[MR[a[i]]] - a[i] < a[i])
edges.pb({a[MR[a[i]]] - a[i],{i,MR[a[i]]}});
for (int j = 2*a[i]; j <= 10000000; j += a[i])
if (m[j] && a[m[j]] - j < a[i])
edges.pb({a[m[j]] - j,{i,m[j]}});
}
sort(edges.begin(),edges.end());
for (int i=0;i<edges.size();i++){
int a = edges[i].s.f,b = edges[i].s.s,c = edges[i].f;
dsu(a,b,c);
}
cout<<ans;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:58:19: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for (int i=0;i<edges.size();i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
37 ms |
39632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
97 ms |
39580 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
40 ms |
39704 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
289 ms |
54916 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
69 ms |
41468 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
683 ms |
91900 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
221 ms |
46816 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
263 ms |
67380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
255 ms |
67232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
70 ms |
43180 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |