#include <bits/stdc++.h>
#define int long long
using namespace std;
const int MAX = 1e7;
int parent[MAX + 1];
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 + 1] = {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 + 1];
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; i++){
for(auto& j : edges[i]){
if(_union(j.first, j.second)) ans += i;
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
240 ms |
391736 KB |
Output is correct |
2 |
Correct |
381 ms |
448100 KB |
Output is correct |
3 |
Correct |
252 ms |
391948 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
168 ms |
313628 KB |
Output is correct |
2 |
Runtime error |
934 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
260 ms |
391920 KB |
Output is correct |
2 |
Correct |
257 ms |
391556 KB |
Output is correct |
3 |
Correct |
274 ms |
392012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
246 ms |
340944 KB |
Output is correct |
2 |
Correct |
371 ms |
397800 KB |
Output is correct |
3 |
Correct |
269 ms |
362932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
197 ms |
325020 KB |
Output is correct |
2 |
Correct |
302 ms |
365928 KB |
Output is correct |
3 |
Correct |
221 ms |
337460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
287 ms |
363860 KB |
Output is correct |
2 |
Correct |
390 ms |
433796 KB |
Output is correct |
3 |
Correct |
261 ms |
359484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
186 ms |
321912 KB |
Output is correct |
2 |
Correct |
391 ms |
433816 KB |
Output is correct |
3 |
Correct |
260 ms |
361900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
359 ms |
416236 KB |
Output is correct |
2 |
Runtime error |
834 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
426012 KB |
Output is correct |
2 |
Runtime error |
898 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
284 ms |
396272 KB |
Output is correct |
2 |
Runtime error |
1124 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |