#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,upd;
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){
for (int i=0;i<upd.size();i++){
edges.pb({upd[i].f,{d[y].s,upd[i].s.s}});
}
}else{
upd.clear();
}
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 = 2*d[y].f; j <= 1e7; j += d[y].f){
if (!m[j]) continue;
edges.pb({a[m[j]] - j,{id,m[j]}});
upd.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:49:27: 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]
49 | for (int i=0;i<upd.size();i++){
| ~^~~~~~~~~~~
sirni.cpp:68: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]
68 | for (int i=0;i<edges.size();i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
39628 KB |
Output is correct |
2 |
Correct |
595 ms |
89068 KB |
Output is correct |
3 |
Correct |
45 ms |
40112 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
101 ms |
39684 KB |
Output is correct |
2 |
Runtime error |
1167 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
41 ms |
39628 KB |
Output is correct |
2 |
Correct |
44 ms |
39568 KB |
Output is correct |
3 |
Correct |
45 ms |
39756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
365 ms |
68972 KB |
Output is correct |
2 |
Correct |
1491 ms |
143520 KB |
Output is correct |
3 |
Correct |
761 ms |
98372 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
93 ms |
43728 KB |
Output is correct |
2 |
Execution timed out |
5094 ms |
436716 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
785 ms |
93644 KB |
Output is correct |
2 |
Correct |
2397 ms |
246208 KB |
Output is correct |
3 |
Correct |
543 ms |
73752 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
284 ms |
47548 KB |
Output is correct |
2 |
Correct |
2322 ms |
244028 KB |
Output is correct |
3 |
Correct |
720 ms |
98304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
338 ms |
69532 KB |
Output is correct |
2 |
Runtime error |
1207 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
443 ms |
76292 KB |
Output is correct |
2 |
Runtime error |
1165 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
73 ms |
43324 KB |
Output is correct |
2 |
Runtime error |
1312 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |