#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int MAX = 1e7 + 7;
int parent[MAX];
int _find(int i){
if(parent[i] < 0) return i;
return parent[i] = _find(parent[i]);
}
bool _union(int u, int v){
u = _find(u), v = _find(v);
if(u == v) return false;
if(parent[u] > parent[v]) swap(u, v);
parent[u] += parent[v];
parent[v] = u;
return true;
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n;
cin>>n;
vector<int> P(n);
for(auto& i : P) cin>>i;
sort(P.begin(), P.end());
int _max = P[n - 1];
int _next[MAX] = {0};
for(auto i : P) _next[i] = i;
for (int i = _max - 1; i >= 0; i--)
{
if(_next[i] != 0) continue;
_next[i] = _next[i + 1];
}
vector<pair<int, int>> edges[MAX];
for(int i = 0; i < n; i++){
if(i != 0 && P[i - 1] == P[i]) continue;
int a = P[i];
if(a == _max) continue;
for (int j = a; j <= _max; j += a)
{
int b;
if(j == P[i]) b = _next[j + 1];
else b = _next[j];
edges[b % a].push_back({a, b});
}
}
memset(parent, -1, sizeof(parent));
int ans = 0;
for(int i = 0; i <= (_max / 2); i++){
for(auto& j : edges[i]){
if(_union(j.first, j.second)) ans += i;
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
313548 KB |
Output is correct |
2 |
Correct |
301 ms |
342788 KB |
Output is correct |
3 |
Correct |
206 ms |
313704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
313500 KB |
Output is correct |
2 |
Correct |
1463 ms |
708072 KB |
Output is correct |
3 |
Correct |
204 ms |
314216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
198 ms |
313456 KB |
Output is correct |
2 |
Correct |
199 ms |
313548 KB |
Output is correct |
3 |
Correct |
206 ms |
313612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
323336 KB |
Output is correct |
2 |
Correct |
309 ms |
351764 KB |
Output is correct |
3 |
Correct |
243 ms |
334340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
315436 KB |
Output is correct |
2 |
Correct |
295 ms |
336612 KB |
Output is correct |
3 |
Correct |
217 ms |
323440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
292 ms |
334776 KB |
Output is correct |
2 |
Correct |
375 ms |
369008 KB |
Output is correct |
3 |
Correct |
236 ms |
332500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
316988 KB |
Output is correct |
2 |
Correct |
356 ms |
369940 KB |
Output is correct |
3 |
Correct |
235 ms |
334072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
323 ms |
326620 KB |
Output is correct |
2 |
Correct |
1888 ms |
671564 KB |
Output is correct |
3 |
Correct |
356 ms |
329316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
351 ms |
330896 KB |
Output is correct |
2 |
Correct |
3377 ms |
785336 KB |
Output is correct |
3 |
Correct |
548 ms |
379800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
222 ms |
315728 KB |
Output is correct |
2 |
Correct |
3491 ms |
672960 KB |
Output is correct |
3 |
Correct |
251 ms |
336152 KB |
Output is correct |