#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],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;
m[10000001] = -1;
for (int i = 1e7; i >= 1; i--){
m[i] = m[i + 1];
while (r >= 0 && i <= d[r].f)
m[i] = r,r--;
}
for (int y = 0; y < n; y++){
if (y > 0 && d[y].f == d[y - 1].f)
edges.pb({0,{y,y - 1}});
if (y + 1 < n) edges.pb({d[y+1].f - d[y].f,{y,y + 1}});
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] < 0) continue;
if (d[m[j]].f - j < d[y].f)
edges.pb({d[m[j]].f - j,{y,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:60: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]
60 | for (int i=0;i<edges.size();i++){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
39596 KB |
Output is correct |
2 |
Correct |
102 ms |
42628 KB |
Output is correct |
3 |
Correct |
45 ms |
39760 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
39628 KB |
Output is correct |
2 |
Correct |
257 ms |
41116 KB |
Output is correct |
3 |
Correct |
45 ms |
39716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
48 ms |
39628 KB |
Output is correct |
2 |
Correct |
47 ms |
39540 KB |
Output is correct |
3 |
Correct |
43 ms |
39756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
315 ms |
66396 KB |
Output is correct |
2 |
Correct |
944 ms |
90844 KB |
Output is correct |
3 |
Correct |
392 ms |
66316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
80 ms |
43000 KB |
Output is correct |
2 |
Correct |
615 ms |
90932 KB |
Output is correct |
3 |
Correct |
406 ms |
66328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
590 ms |
91020 KB |
Output is correct |
2 |
Correct |
1218 ms |
140240 KB |
Output is correct |
3 |
Correct |
381 ms |
66384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
216 ms |
46680 KB |
Output is correct |
2 |
Correct |
1194 ms |
140224 KB |
Output is correct |
3 |
Correct |
352 ms |
66332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
273 ms |
66356 KB |
Output is correct |
2 |
Correct |
4259 ms |
436468 KB |
Output is correct |
3 |
Correct |
325 ms |
67128 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
260 ms |
66432 KB |
Output is correct |
2 |
Execution timed out |
5098 ms |
436396 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
76 ms |
42944 KB |
Output is correct |
2 |
Execution timed out |
5088 ms |
436352 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |