#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]);
}
P.resize(unique(P.begin(),P.end())-P.begin());
N = P.size();
D.init(N);
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:59:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<edge>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
59 | for(int i = 0;i<edges.size();++i){
| ~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
44 ms |
3536 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
562 ms |
2132 KB |
Output is correct |
3 |
Correct |
3 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
604 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
14016 KB |
Output is correct |
2 |
Correct |
601 ms |
100188 KB |
Output is correct |
3 |
Correct |
242 ms |
26424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
3792 KB |
Output is correct |
2 |
Correct |
2422 ms |
395636 KB |
Output is correct |
3 |
Correct |
155 ms |
26520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
329 ms |
50964 KB |
Output is correct |
2 |
Correct |
953 ms |
100356 KB |
Output is correct |
3 |
Correct |
175 ms |
26500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
7136 KB |
Output is correct |
2 |
Correct |
808 ms |
100176 KB |
Output is correct |
3 |
Correct |
212 ms |
26500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
26588 KB |
Output is correct |
2 |
Correct |
3812 ms |
395820 KB |
Output is correct |
3 |
Correct |
205 ms |
26672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
162 ms |
26536 KB |
Output is correct |
2 |
Runtime error |
2829 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
3796 KB |
Output is correct |
2 |
Runtime error |
2439 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |