#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;
assert(tmp>=1);
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:39: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]
39 | for(int i=0; i<a.size(); ++i) T[a[i]] = i;
| ~^~~~~~~~~
sirni.cpp:46: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]
46 | for(int i=0; i<a.size()-1; ++i) {
| ~^~~~~~~~~~~
sirni.cpp:54: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]
54 | for(int i=0; i<pq.size(); ++i) {
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
78956 KB |
Output is correct |
2 |
Correct |
295 ms |
177136 KB |
Output is correct |
3 |
Correct |
35 ms |
79184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
800 KB |
Output is correct |
2 |
Runtime error |
500 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
35 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 |
60852 KB |
Output is correct |
2 |
Correct |
621 ms |
110248 KB |
Output is correct |
3 |
Correct |
298 ms |
110248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
16844 KB |
Output is correct |
2 |
Correct |
362 ms |
110644 KB |
Output is correct |
3 |
Correct |
176 ms |
57844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
415 ms |
111008 KB |
Output is correct |
2 |
Correct |
868 ms |
208652 KB |
Output is correct |
3 |
Correct |
265 ms |
62288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
16600 KB |
Output is correct |
2 |
Correct |
869 ms |
208544 KB |
Output is correct |
3 |
Correct |
286 ms |
61872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
240 ms |
131756 KB |
Output is correct |
2 |
Runtime error |
528 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
132276 KB |
Output is correct |
2 |
Runtime error |
574 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 |
664 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |