#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];
parent[i] = i;
siz[i] = 1;
}
sort(a, a+n);
for(int i=0; i<n; ++i) T[a[i]] = i + 1;
for(int i=a[n-1]; i>0; --i) {
if(T[i]) continue;
T[i] = T[i+1];
}
int ans = 0;
vector<pair<int,pair<int,int>>>pq;
for(int i=0; i<n-1; ++i) {
for(int j=a[i]; j<=a[n-1]; j+=a[i]) {
int x, p;
if(j==a[i]) x = a[T[a[i]+1]-1], p = T[a[i]+1];
else x = a[T[j]-1], p = T[j];
if(x > j+a[i]) continue;
pq.push_back({x%a[i], {p-1, i}});
}
}
sort(pq.begin(), pq.end());
int added = 1;
for(int i=0; i<pq.size(); ++i) {
if(added == n) break;
int w = pq[i].first, x = pq[i].second.first, y = pq[i].second.second;
if(find_parent(x) == find_parent(y)) continue;
++added;
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 |
32 ms |
80212 KB |
Output is correct |
2 |
Incorrect |
92 ms |
87288 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
2652 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
80220 KB |
Output is correct |
2 |
Incorrect |
39 ms |
79956 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
177 ms |
61872 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
24 ms |
18888 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
416 ms |
110488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
18632 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
225 ms |
132260 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
230 ms |
131244 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
87232 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |