#include <bits/stdc++.h>
using namespace std;
struct DSU{
int N;
vector<int> d;
void init(int n){
N = n;
d = vector<int>(N,-1);
}
int finden(int x){
if(d[x] < 0) return x;
return finden(d[x]);
}
bool unite(int a, int b){
a = finden(a), b = finden(b);
if(a == b) return false;
if(d[a] > d[b]){
swap(a,b);
} //absolute of d[a] is smaller than absolute of d[b], so there are less in a than in b, although we want to add b to a
d[a] += d[b]; d[b] = a;
return true;
}
}D;
struct edge{
int u,v,w;
bool operator<(const edge &b){
return w < b.w;
}
};
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N; cin >> N;
vector<int> P(N);
int maxx = 0;
for(int i = 0;i<N;++i){
cin >> P[i];
maxx = max(maxx,P[i]);
}
D.init(N);
P.resize(unique(P.begin(),P.end())-P.begin());
sort(P.begin(),P.end());
vector<edge> edges;
for(int i = 0;i<N;++i){
for(int j = P[i];j <= maxx; j += P[i]){
auto it = lower_bound(P.begin()+i+1,P.end(),j);
if(it == P.end()) continue;
int it2 = it-P.begin();
if(P[it2]-j >= P[i]){
continue;
}
edge e; e.u = i; e.v = it2; e.w = P[it2]-j;
edges.push_back(e);
}
}
sort(edges.begin(),edges.end());
int ans = 0, cnt = N-1;
for(int i = 0;i<edges.size();++i){
if(D.unite(edges[i].u,edges[i].v)){
ans += edges[i].w;
--cnt;
}
if(cnt == 0) break;
}
cout << ans << "\n";
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:58:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
58 | for(int i = 0;i<edges.size();++i){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Incorrect |
45 ms |
3532 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
604 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
134 ms |
14312 KB |
Output is correct |
2 |
Correct |
591 ms |
100396 KB |
Output is correct |
3 |
Incorrect |
230 ms |
26584 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
3792 KB |
Output is correct |
2 |
Incorrect |
2245 ms |
395664 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
328 ms |
51344 KB |
Output is correct |
2 |
Incorrect |
911 ms |
100284 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
7104 KB |
Output is correct |
2 |
Correct |
774 ms |
100440 KB |
Output is correct |
3 |
Incorrect |
201 ms |
26580 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
160 ms |
26736 KB |
Output is correct |
2 |
Correct |
3732 ms |
396020 KB |
Output is correct |
3 |
Correct |
196 ms |
26668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
161 ms |
26532 KB |
Output is correct |
2 |
Runtime error |
2821 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
3796 KB |
Output is correct |
2 |
Runtime error |
2509 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |