#include <bits/stdc++.h>
#define ii pair<int, int>
#define fi first
#define se second
using namespace std;
using ll = long long;
const int nx=1e5+5;
const int N=2e7+5;
int n, a[nx], last[N], par[nx];
ll ans=0;
vector<int> rar;
vector<pair<int, ii>> ed;
//int find(int u)
//{
// if(!par[u])
// return u;
// return par[u]=find(par[u]);
//}
//bool join(int u, int v)
//{
// u=find(u);
// v=find(v);
// if(u==v)
// return 0;
// par[v]=u;
// return 1;
//}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
function<int(int)> find = [&](int u) -> int {
if(!par[u]) return u;
return par[u]=find(par[u]);
};
function<bool(int, int)> join = [&](int u, int v) -> int {
u = find(u); v = find(v);
if(u == v)return 0;
par[v] = u;
return 1;
};
cin>>n;
for(int i = 1; i <= n; i++)
cin>>a[i], rar.emplace_back(a[i]);
sort(rar.begin(), rar.end());
rar.erase(unique(rar.begin(), rar.end()), rar.end());
n=rar.size();
for(int i = 1; i <= n; i++)
{
a[i]=rar[i-1];
last[a[i]]=i;
}
for(int i = N-2; i >= 1; i--)
{
if(last[i])
continue;
if(last[i+1])
last[i]=last[i+1];
}
for(int i = 1; i <= n; i++)
{
if(last[a[i]+1])
ed.push_back({a[last[a[i]+1]]%a[i], {i, last[a[i]+1]}});
for(int j = 2*a[i]; j < N; j+=a[i])
if(last[j])
ed.push_back({a[last[j]]%a[i], {i, last[j]}});
}
sort(ed.begin(), ed.end());
for(auto it:ed)
if(join(it.se.fi, it.se.se))
ans+=it.fi;
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
21 ms |
41552 KB |
Output is correct |
2 |
Correct |
243 ms |
92844 KB |
Output is correct |
3 |
Correct |
31 ms |
42000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
2756 KB |
Output is correct |
2 |
Runtime error |
642 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
41552 KB |
Output is correct |
2 |
Correct |
26 ms |
41552 KB |
Output is correct |
3 |
Correct |
26 ms |
41800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
217 ms |
34572 KB |
Output is correct |
2 |
Correct |
709 ms |
59088 KB |
Output is correct |
3 |
Correct |
332 ms |
59132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
10496 KB |
Output is correct |
2 |
Correct |
392 ms |
59164 KB |
Output is correct |
3 |
Correct |
230 ms |
32304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
431 ms |
59164 KB |
Output is correct |
2 |
Correct |
976 ms |
108308 KB |
Output is correct |
3 |
Correct |
296 ms |
34604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
109 ms |
10040 KB |
Output is correct |
2 |
Correct |
986 ms |
108308 KB |
Output is correct |
3 |
Correct |
292 ms |
34540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
69424 KB |
Output is correct |
2 |
Runtime error |
745 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
277 ms |
69428 KB |
Output is correct |
2 |
Runtime error |
728 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
45308 KB |
Output is correct |
2 |
Runtime error |
795 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |