#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 10000001, MAX_SIZ = 100000;
int parent[MAX_SIZ], siz[MAX_SIZ];
int find_parent(int x) {
if(parent[x] == x) return x;
return parent[x] = find_parent(parent[x]);
}
void unite(int a, int b) {
int x = find_parent(a), y = find_parent(b);
if(x==y) return;
if(siz[x] < siz[y]) swap(x,y);
siz[x] += siz[y];
parent[y] = x;
}
void solve() {
int n;
cin >> n;
set<int>s;
for(int i=0; i<n; ++i) {
int tmp;
cin >> tmp;
s.insert(tmp);
parent[i] = i;
siz[i] = 1;
}
vector<int>a(s.begin(), s.end()), T(a[n-1]+1, -1);
n = a.size();
for(int i=0; i<n; ++i) T[a[i]] = i;
for(int i=a[n-1]; i>0; --i) {
if(T[i] != -1) continue;
T[i] = T[i+1];
}
int ans = 0;
vector<pair<int,pair<int,int>>>pq;
for(int i=0; i<n-1; ++i) {
pq.push_back({a[i+1]%a[i], {i+1, i}});
for(int j=2*a[i]; j<=a[n-1]; j+=a[i]) {
int p = T[j];
pq.push_back({a[p]%a[i], {p, i}});
}
}
sort(pq.begin(), pq.end());
for(int i=0; i<pq.size(); ++i) {
int w = pq[i].first, x = pq[i].second.first, y = pq[i].second.second;
if(find_parent(x) == find_parent(y)) continue;
unite(x, y);
ans += w;
}
cout << ans << endl;
}
signed main()
{
int t=1;
while(t--) solve();
return 0;
}
Compilation message
sirni.cpp: In function 'void solve()':
sirni.cpp:51:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i=0; i<pq.size(); ++i) {
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
78928 KB |
Output is correct |
2 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
78940 KB |
Output is correct |
2 |
Runtime error |
1 ms |
600 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
53 ms |
14168 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
7 ms |
2652 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
49 ms |
14164 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
17 ms |
5468 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
57 ms |
14660 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
53 ms |
14672 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
10 ms |
3420 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |