#include <bits/stdc++.h>
#define int 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;
}
signed 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; 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];
if(j / a == b / a) 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 |
230 ms |
391880 KB |
Output is correct |
2 |
Correct |
275 ms |
396576 KB |
Output is correct |
3 |
Correct |
232 ms |
392012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
391876 KB |
Output is correct |
2 |
Correct |
935 ms |
393232 KB |
Output is correct |
3 |
Correct |
244 ms |
391984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
229 ms |
391760 KB |
Output is correct |
2 |
Correct |
230 ms |
391736 KB |
Output is correct |
3 |
Correct |
389 ms |
391920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
410952 KB |
Output is correct |
2 |
Correct |
367 ms |
464568 KB |
Output is correct |
3 |
Correct |
283 ms |
421716 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
394856 KB |
Output is correct |
2 |
Correct |
301 ms |
434784 KB |
Output is correct |
3 |
Correct |
247 ms |
411312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
308 ms |
433732 KB |
Output is correct |
2 |
Correct |
409 ms |
486560 KB |
Output is correct |
3 |
Correct |
270 ms |
418400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
213 ms |
398536 KB |
Output is correct |
2 |
Correct |
394 ms |
483284 KB |
Output is correct |
3 |
Correct |
269 ms |
417696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
316 ms |
414492 KB |
Output is correct |
2 |
Runtime error |
1231 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
325 ms |
415412 KB |
Output is correct |
2 |
Runtime error |
1430 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
396108 KB |
Output is correct |
2 |
Runtime error |
1309 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |