#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 |
210 ms |
313564 KB |
Output is correct |
2 |
Correct |
305 ms |
342876 KB |
Output is correct |
3 |
Correct |
230 ms |
313708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
313456 KB |
Output is correct |
2 |
Correct |
1493 ms |
708308 KB |
Output is correct |
3 |
Correct |
206 ms |
314232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
199 ms |
313456 KB |
Output is correct |
2 |
Correct |
214 ms |
313460 KB |
Output is correct |
3 |
Correct |
203 ms |
313680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
226 ms |
323352 KB |
Output is correct |
2 |
Correct |
333 ms |
351732 KB |
Output is correct |
3 |
Correct |
248 ms |
334340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
315324 KB |
Output is correct |
2 |
Correct |
286 ms |
336504 KB |
Output is correct |
3 |
Correct |
209 ms |
323384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
276 ms |
334640 KB |
Output is correct |
2 |
Correct |
357 ms |
369040 KB |
Output is correct |
3 |
Correct |
248 ms |
332420 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
184 ms |
316904 KB |
Output is correct |
2 |
Correct |
420 ms |
369804 KB |
Output is correct |
3 |
Correct |
257 ms |
334108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
350 ms |
326720 KB |
Output is correct |
2 |
Correct |
2211 ms |
671636 KB |
Output is correct |
3 |
Correct |
373 ms |
329392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
423 ms |
330760 KB |
Output is correct |
2 |
Correct |
3616 ms |
785244 KB |
Output is correct |
3 |
Correct |
555 ms |
379736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
228 ms |
315732 KB |
Output is correct |
2 |
Correct |
3452 ms |
672752 KB |
Output is correct |
3 |
Correct |
249 ms |
336176 KB |
Output is correct |