#include<bits/stdc++.h>
#define ll long long
using namespace std;
int a[100009];
int p[100009],s[100009];
int N[10000009];
vector<pair<int,pair<int,int> > > E;
void ad(int x, int y){
if(x != y)
E.push_back({min(a[x]%a[y], a[y]%a[x]), {x,y}});
}
int P(int x){
if(p[x] == x) return x;
p[x] = P(p[x]);
return p[x];
}
ll ans = 0;
void un(int x, int y, int z){
x = P(x); y = P(y);
if(x == y) return;
if(s[x] > s[y]) swap(x, y);
p[x] = y;
s[x] += s[y];
ans += z;
}
main(){
int n;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
p[i] = i;
s[i] = 1;
}
sort(a+1, a+n+1);
vector<int> v;
for(int i = 1;i <= n; i++){
if(a[i] == a[i-1]) continue;
N[a[i]] = i;
v.push_back(i);
}
for(int i = a[n]; i >= 0; i--){
if(!N[i]) N[i] = N[i+1];
}
for(int i = 1; i < v.size(); i++){
int x = v[i];
for(int j = 0; j * a[x] <= a[n]; j++){
int y = N[j*a[x]];
if(y == x && a[y] < a[n]) y = N[a[y]+1];
ad(x, y);
}
}
sort(E.begin(), E.end());
for(auto e: E){
int x = e.second.first, y = e.second.second;
un(x, y, e.first);
}
cout << ans << endl;
}
Compilation message
sirni.cpp:26:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
26 | main(){
| ^~~~
sirni.cpp: In function 'int main()':
sirni.cpp:45:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i = 1; i < v.size(); i++){
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
39500 KB |
Output is correct |
2 |
Correct |
361 ms |
88564 KB |
Output is correct |
3 |
Correct |
49 ms |
39736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Runtime error |
1297 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
39588 KB |
Output is correct |
2 |
Correct |
50 ms |
39396 KB |
Output is correct |
3 |
Correct |
54 ms |
39692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
247 ms |
31372 KB |
Output is correct |
2 |
Correct |
777 ms |
55940 KB |
Output is correct |
3 |
Correct |
353 ms |
31296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
7796 KB |
Output is correct |
2 |
Correct |
406 ms |
55264 KB |
Output is correct |
3 |
Correct |
217 ms |
29320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
452 ms |
55948 KB |
Output is correct |
2 |
Correct |
958 ms |
105416 KB |
Output is correct |
3 |
Correct |
277 ms |
31408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
8120 KB |
Output is correct |
2 |
Correct |
968 ms |
105164 KB |
Output is correct |
3 |
Correct |
291 ms |
31404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
327 ms |
66840 KB |
Output is correct |
2 |
Runtime error |
1229 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
66800 KB |
Output is correct |
2 |
Runtime error |
1142 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
43024 KB |
Output is correct |
2 |
Runtime error |
1438 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |