#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);
for(int i = 0;i<N;++i) cin >> P[i];
D.init(N);
sort(P.begin(),P.end());
vector<edge> edges;
for(int i = 0;i<N;++i){
for(int j = i+1;j<N;++j){ //P[j] >= P[i]
edge e; e.u = i; e.v = j; e.w = P[j]%P[i];
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:47:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
47 | for(int i = 0;i<edges.size();++i){
| ~^~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
6604 KB |
Output is correct |
2 |
Correct |
29 ms |
6604 KB |
Output is correct |
3 |
Correct |
35 ms |
6764 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
6600 KB |
Output is correct |
2 |
Correct |
28 ms |
6604 KB |
Output is correct |
3 |
Correct |
35 ms |
6604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
6600 KB |
Output is correct |
2 |
Correct |
28 ms |
6600 KB |
Output is correct |
3 |
Correct |
37 ms |
6600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
729 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
698 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
698 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
679 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
693 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
705 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
721 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |