#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX_N = 10000001, MAX_SIZ = 100000;
int T[MAX_N], 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;
int a[n];
for(int i=0; i<n; ++i) {
cin >> a[i];
if(a[i] == a[i-1]) {
--n, --i;
continue;
}
parent[i] = i;
siz[i] = 1;
}
sort(a, a+n);
memset(T, -1, sizeof T);
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: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 |
33 ms |
80208 KB |
Output is correct |
2 |
Correct |
353 ms |
178592 KB |
Output is correct |
3 |
Correct |
35 ms |
80888 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
80408 KB |
Output is correct |
2 |
Runtime error |
553 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
80212 KB |
Output is correct |
2 |
Correct |
33 ms |
80212 KB |
Output is correct |
3 |
Correct |
34 ms |
80468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
193 ms |
131780 KB |
Output is correct |
2 |
Correct |
916 ms |
280208 KB |
Output is correct |
3 |
Correct |
412 ms |
182152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
37 ms |
86472 KB |
Output is correct |
2 |
Runtime error |
533 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
441 ms |
181432 KB |
Output is correct |
2 |
Correct |
1613 ms |
475692 KB |
Output is correct |
3 |
Correct |
295 ms |
132200 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
93372 KB |
Output is correct |
2 |
Correct |
1480 ms |
477424 KB |
Output is correct |
3 |
Correct |
380 ms |
181680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
242 ms |
131764 KB |
Output is correct |
2 |
Runtime error |
559 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
288 ms |
131380 KB |
Output is correct |
2 |
Runtime error |
530 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
87200 KB |
Output is correct |
2 |
Runtime error |
569 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |