#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=1e7+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);
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++)
{
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 |
15 ms |
39760 KB |
Output is correct |
2 |
Correct |
201 ms |
91000 KB |
Output is correct |
3 |
Correct |
14 ms |
40012 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
17 ms |
2640 KB |
Output is correct |
2 |
Runtime error |
669 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
39760 KB |
Output is correct |
2 |
Correct |
18 ms |
39760 KB |
Output is correct |
3 |
Correct |
13 ms |
40092 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
34368 KB |
Output is correct |
2 |
Correct |
613 ms |
59252 KB |
Output is correct |
3 |
Correct |
299 ms |
58928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
10484 KB |
Output is correct |
2 |
Correct |
315 ms |
58928 KB |
Output is correct |
3 |
Correct |
192 ms |
32196 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
352 ms |
58920 KB |
Output is correct |
2 |
Correct |
788 ms |
108216 KB |
Output is correct |
3 |
Correct |
244 ms |
34360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
70 ms |
10044 KB |
Output is correct |
2 |
Correct |
794 ms |
108404 KB |
Output is correct |
3 |
Correct |
237 ms |
34356 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
67800 KB |
Output is correct |
2 |
Runtime error |
604 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
233 ms |
67644 KB |
Output is correct |
2 |
Runtime error |
674 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
43780 KB |
Output is correct |
2 |
Runtime error |
665 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |