#include <bits/stdc++.h>
//#include <iostream>
//#include <vector>
#define s second
#define pb push_back
#define f first
using namespace std;
const int N = 100005,M = 10000005;
int p[N],a[N],MR[N],ans;
int m[M];
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 (pa < pb) swap(pa,pb);
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,d.pb({a[i],i});
sort(d.begin(),d.end());
int r = d.size() - 1;
for (int i = 1e7; i >= 1; i--){
m[i] = m[i + 1];
while (r >= 0 && i <= d[r].f)
m[i] = d[r--].s;
}
for(int i = n - 2; i >= 0; i--)
MR[d[i].s] = d[i + 1].s;
for (int y = 0; y < n; y++){
if (y > 0 && d[y].f == d[y - 1].f){
edges.pb({0,{d[y].s,d[y - 1].s}});
}
int id = d[y].s;
if (MR[id]) edges.pb({a[MR[id]] - a[id],{id,MR[id]}});
if (y > 0 && d[y].f == d[y - 1].f) continue;
for (int j = d[y].f; j <= 1e7; j += d[y].f){
if (!m[j]) continue;
edges.pb({a[m[j]] - j,{id,m[j]}});
}
}
sort(edges.begin(),edges.end());
for (int i=0;i<edges.size();i++){
int x = edges[i].s.f,y = edges[i].s.s,c = edges[i].f;
dsu(x,y,c);
}
cout<<ans;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:63: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]
63 | for (int i=0;i<edges.size();i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
40 ms |
39636 KB |
Output is correct |
2 |
Correct |
464 ms |
88840 KB |
Output is correct |
3 |
Correct |
48 ms |
39940 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
39700 KB |
Output is correct |
2 |
Runtime error |
845 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
39708 KB |
Output is correct |
2 |
Correct |
37 ms |
39564 KB |
Output is correct |
3 |
Correct |
43 ms |
39752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
380 ms |
66820 KB |
Output is correct |
2 |
Correct |
1129 ms |
140556 KB |
Output is correct |
3 |
Correct |
590 ms |
91460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
42980 KB |
Output is correct |
2 |
Correct |
686 ms |
91420 KB |
Output is correct |
3 |
Correct |
443 ms |
67320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
699 ms |
91472 KB |
Output is correct |
2 |
Correct |
1502 ms |
140588 KB |
Output is correct |
3 |
Correct |
485 ms |
66772 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
274 ms |
46676 KB |
Output is correct |
2 |
Correct |
1598 ms |
140664 KB |
Output is correct |
3 |
Correct |
568 ms |
91536 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
304 ms |
66748 KB |
Output is correct |
2 |
Runtime error |
938 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
379 ms |
66804 KB |
Output is correct |
2 |
Runtime error |
935 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
75 ms |
43000 KB |
Output is correct |
2 |
Runtime error |
1097 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |