#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;
vector <int> v[N];
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}),v[a[i]].pb(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 = 1; y <= 1e7; y++){
for (int id: v[y])
if (MR[id]) edges.pb({a[MR[id]] - a[id],{id,MR[id]}});
for (int j = 2*y; j <= 1e7; j += y){
if (!m[j]) continue;
for (int id: v[y])
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:61: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]
61 | for (int i=0;i<edges.size();i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1035 ms |
274484 KB |
Output is correct |
2 |
Correct |
1639 ms |
323684 KB |
Output is correct |
3 |
Correct |
1134 ms |
274848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
922 ms |
274672 KB |
Output is correct |
2 |
Runtime error |
1002 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1145 ms |
274616 KB |
Output is correct |
2 |
Correct |
1131 ms |
274356 KB |
Output is correct |
3 |
Correct |
1091 ms |
274620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1150 ms |
305056 KB |
Output is correct |
2 |
Correct |
2080 ms |
378440 KB |
Output is correct |
3 |
Correct |
1379 ms |
329048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
941 ms |
278276 KB |
Output is correct |
2 |
Correct |
4849 ms |
672324 KB |
Output is correct |
3 |
Correct |
1174 ms |
304300 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1421 ms |
329772 KB |
Output is correct |
2 |
Correct |
2764 ms |
477464 KB |
Output is correct |
3 |
Correct |
1239 ms |
304952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
947 ms |
282900 KB |
Output is correct |
2 |
Correct |
2609 ms |
476720 KB |
Output is correct |
3 |
Correct |
1376 ms |
329252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1323 ms |
305132 KB |
Output is correct |
2 |
Runtime error |
993 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1339 ms |
305112 KB |
Output is correct |
2 |
Runtime error |
976 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1045 ms |
278604 KB |
Output is correct |
2 |
Runtime error |
1307 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |