#include <bits/stdc++.h>
#define ff first
#define ss second
#define pb push_back
#define all(x) x.begin(),x.end()
using namespace std;
const int N=1e5+5,M=1e7+5;
int p[N],ind[M],nx[M];
vector <pair <int,int> > g[M];
int find_set(int v){
if(p[v]==v)return v;
return p[v]=find_set(p[v]);
}
bool union_set(int u,int v){
u=find_set(u);
v=find_set(v);
if(u==v)return false;
p[u]=v;
return true;
}
signed main(){
ios_base::sync_with_stdio();
cin.tie(0);cout.tie(0);
int n;
cin>>n;
vector <int> a(n);
for(int i=0;i<n;i++)cin>>a[i];
vector < vector <int> > v;
sort(all(a));
for(int i=0;i<n;i++){
if(i-1>=0 && a[i]==a[i-1])continue;
nx[a[i]]=a[i];
ind[a[i]]=i;
}
for(int i=M-2;i>=0;i--){
if(!nx[i])nx[i]=nx[i+1];
}
for(int i=0;i<n;i++){
if(i-1>=0 && a[i]==a[i-1])continue;
for(int j=a[i];j<M;j+=a[i]){
int nxt=0;
if(j==a[i])nxt=nx[j+1];
else nxt=nx[j];
if(!nxt)break;
g[min(nxt%a[i],a[i]%nxt)].pb({ind[nxt],i});
}
}
for(int i=0;i<n;i++)p[i]=i;
int ans=0;
for(int i=0;i<M;i++){
for(auto x : g[i]){
if(union_set(x.ff,x.ss))ans+=i;
}
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
143 ms |
278108 KB |
Output is correct |
2 |
Correct |
245 ms |
305848 KB |
Output is correct |
3 |
Correct |
148 ms |
278868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
274520 KB |
Output is correct |
2 |
Correct |
1263 ms |
669488 KB |
Output is correct |
3 |
Correct |
158 ms |
279048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
144 ms |
278436 KB |
Output is correct |
2 |
Correct |
143 ms |
276812 KB |
Output is correct |
3 |
Correct |
145 ms |
278468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
190 ms |
288396 KB |
Output is correct |
2 |
Correct |
252 ms |
317548 KB |
Output is correct |
3 |
Correct |
209 ms |
300268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
280148 KB |
Output is correct |
2 |
Correct |
212 ms |
301312 KB |
Output is correct |
3 |
Correct |
247 ms |
288488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
300260 KB |
Output is correct |
2 |
Correct |
284 ms |
334900 KB |
Output is correct |
3 |
Correct |
207 ms |
298348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
278604 KB |
Output is correct |
2 |
Correct |
287 ms |
335764 KB |
Output is correct |
3 |
Correct |
208 ms |
299928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
237 ms |
326992 KB |
Output is correct |
2 |
Correct |
1517 ms |
672792 KB |
Output is correct |
3 |
Correct |
255 ms |
330528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
331936 KB |
Output is correct |
2 |
Correct |
2097 ms |
785148 KB |
Output is correct |
3 |
Correct |
403 ms |
387696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
311124 KB |
Output is correct |
2 |
Correct |
1968 ms |
665944 KB |
Output is correct |
3 |
Correct |
213 ms |
302048 KB |
Output is correct |