#include<bits/stdc++.h>
using namespace std;
const int N = 1e6 + 5;
const int nmax = 1e7 + 1;
using ll = long long;
int n;
vector<int> g[N];
vector<int> v;
int root[nmax + 5];
int nxt[nmax + 5];
struct tt{
int x;
int y;
int z;
};
vector<tt> edge;
bool cmp(tt x, tt y){
return x.z < y.z;
}
inline int find(int u) { return (u == root[u] ? u : root[u] = find(root[u])); }
signed main(){
cin>>n;
for(int i = 1 , x ; i <= n ; ++i){
cin>>x;
v.push_back(x);
g[x].push_back(i);
}
sort(v.begin(),v.end());
v.erase(unique(v.begin(),v.end()),v.end());
int j = 0;
for(int i = 1 ; i <= v.back() ; ++i){
while(i > v[j] ) j++;
nxt[i] = v[j];
}
for (int x : v) for (int y = x; y <= v.back(); y += x) {
int p = nxt[(y==x?y+1:y)];
if (!p) break;
if (y + x < p) y = (p - y) / x * x + y;
edge.push_back({x, p, p - y});
}
for(auto i : v) root[i] = i;
sort(edge.begin(),edge.end(),cmp);
ll ans = 0;
for (tt ee : edge) {
int u = ee.x, v = ee.y, c = ee.z;
//cout<<u<<'.'<<v<<'.'<<c<<"\n";
u = find(u), v = find(v);
if (u != v) {
root[u] = v;
ans += 1ll*c;
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
16 ms |
48220 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
28248 KB |
Output is correct |
2 |
Runtime error |
16 ms |
48320 KB |
Execution killed with signal 11 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
17 ms |
48216 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
102 ms |
49972 KB |
Output is correct |
2 |
Correct |
276 ms |
85024 KB |
Output is correct |
3 |
Correct |
121 ms |
56372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
36552 KB |
Output is correct |
2 |
Correct |
190 ms |
79156 KB |
Output is correct |
3 |
Correct |
96 ms |
45620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
191 ms |
81712 KB |
Output is correct |
2 |
Correct |
338 ms |
130852 KB |
Output is correct |
3 |
Correct |
114 ms |
57404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
42 ms |
36760 KB |
Output is correct |
2 |
Correct |
338 ms |
130688 KB |
Output is correct |
3 |
Correct |
126 ms |
56848 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
16 ms |
48352 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
15 ms |
48220 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
17 ms |
48284 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |