#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 100000;
int parent[MAX_N], siz[MAX_N];
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;
vector<int>a;
for(int i=0; i<n; ++i) {
int tmp;
cin >> tmp;
a.push_back(tmp);
parent[i] = i;
siz[i] = 1;
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()), a.end());
int largest = a.back();
vector<int>T(largest+1, -1);
for(int i=0; i<a.size(); ++i) T[a[i]] = i;
for(int i=largest-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<a.size()-1; ++i) {
pq.push_back({a[i+1]%a[i], {i+1, i}});
for(int j=2*a[i]; j<=largest; 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:38:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int i=0; i<a.size(); ++i) T[a[i]] = i;
| ~^~~~~~~~~
sirni.cpp:45:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for(int i=0; i<a.size()-1; ++i) {
| ~^~~~~~~~~~~
sirni.cpp:53: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]
53 | for(int i=0; i<pq.size(); ++i) {
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
74 ms |
78928 KB |
Output is correct |
2 |
Correct |
332 ms |
178780 KB |
Output is correct |
3 |
Correct |
37 ms |
79184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
796 KB |
Output is correct |
2 |
Runtime error |
561 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
78928 KB |
Output is correct |
2 |
Correct |
33 ms |
78676 KB |
Output is correct |
3 |
Correct |
34 ms |
79136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
62376 KB |
Output is correct |
2 |
Correct |
630 ms |
110996 KB |
Output is correct |
3 |
Correct |
296 ms |
110756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
31 ms |
16844 KB |
Output is correct |
2 |
Correct |
348 ms |
110896 KB |
Output is correct |
3 |
Correct |
173 ms |
56700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
374 ms |
110728 KB |
Output is correct |
2 |
Correct |
875 ms |
208956 KB |
Output is correct |
3 |
Correct |
264 ms |
60628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
17476 KB |
Output is correct |
2 |
Correct |
878 ms |
209800 KB |
Output is correct |
3 |
Correct |
288 ms |
60592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
132444 KB |
Output is correct |
2 |
Runtime error |
525 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
133028 KB |
Output is correct |
2 |
Runtime error |
532 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
86588 KB |
Output is correct |
2 |
Runtime error |
622 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |