#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[N], last[N], par[N];
ll ans=0;
vector<int> rar;
vector<ii> ed[N];
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++)
{
if(last[a[i]+1])
ed[a[last[a[i]+1]]%a[i]].emplace_back(i, last[a[i]+1]);
for(int j = 2*a[i]; j <= a[n]; j+=a[i])
if(last[j])
ed[a[last[j]]%a[i]].emplace_back(i, last[j]);
}
for(int i = 0; i < N; i++)
for(auto it:ed[i])
if(join(it.fi, it.se))
ans+=i;
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
73 ms |
277576 KB |
Output is correct |
2 |
Correct |
118 ms |
306944 KB |
Output is correct |
3 |
Correct |
66 ms |
277956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
240784 KB |
Output is correct |
2 |
Correct |
617 ms |
671984 KB |
Output is correct |
3 |
Correct |
63 ms |
278344 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
277576 KB |
Output is correct |
2 |
Correct |
63 ms |
277584 KB |
Output is correct |
3 |
Correct |
66 ms |
277840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
90 ms |
254904 KB |
Output is correct |
2 |
Correct |
135 ms |
289864 KB |
Output is correct |
3 |
Correct |
99 ms |
267204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
61 ms |
246724 KB |
Output is correct |
2 |
Correct |
112 ms |
267876 KB |
Output is correct |
3 |
Correct |
89 ms |
253628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
123 ms |
271004 KB |
Output is correct |
2 |
Correct |
147 ms |
306308 KB |
Output is correct |
3 |
Correct |
114 ms |
265652 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
68 ms |
244272 KB |
Output is correct |
2 |
Correct |
178 ms |
303764 KB |
Output is correct |
3 |
Correct |
109 ms |
268304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
129 ms |
290760 KB |
Output is correct |
2 |
Correct |
727 ms |
640764 KB |
Output is correct |
3 |
Correct |
125 ms |
294600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
295392 KB |
Output is correct |
2 |
Correct |
999 ms |
773320 KB |
Output is correct |
3 |
Correct |
185 ms |
353104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
279892 KB |
Output is correct |
2 |
Correct |
1000 ms |
640428 KB |
Output is correct |
3 |
Correct |
114 ms |
273304 KB |
Output is correct |