//#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long llo;
#define mp make_pair
#define pb push_back
#define a first
#define b second
#define endl '\n'
int n;
bool vis[10000001];
int ind[10000001];
int re[10000001+1];
int par[100001];
int find(int no){
if(par[no]==no){
return no;
}
par[no]=find(par[no]);
return par[no];
}
vector<pair<int,int>> pre[10000001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
int ind2=0;
vector<int> it;
for(int i=0;i<n;i++){
int x;
cin>>x;
if(vis[x]==0){
vis[x]=1;
ind[x]=ind2;
it.pb(x);
//cout<<x<<":"<<ind2<<endl;
ind2++;
}
}
for(int i=0;i<ind2;i++){
par[i]=i;
}
/*for(int i=0;i<ind2;i++){
for(int j=0;j<ind2;j++){
if(it[i]>it[j]){
pre[it[i]%it[j]].pb({i,j});
}
}
}*/
re[10000001]=-1;
for(int i=10000000;i>=1;i--){
re[i]=re[i+1];
if(vis[i]==1){
re[i]=i;
}
}
for(int i=1;i<=10000000-1;i++){
if(vis[i]==1){
for(int j=i;j<=10000000;j+=i){
if(re[j]>=0){
if(re[j]==i){
if(re[j+1]>=0){
//cout<<re[j+1]-j<<":"<<re[j+1]<<":"<<i<<endl;
pre[re[j+1]-j].pb({ind[re[j+1]],ind[i]});
continue;
}
}
if(re[j]-j>=i){
continue;
}
pre[re[j]-j].pb({ind[re[j]],ind[i]});
}
}
}
}
llo ans=0;
for(int i=0;i<=10000000;i++){
for(auto j:pre[i]){
int x=find(j.a);
int y=find(j.b);
/*if(i==0){
cout<<it[j.a]<<","<<it[j.b]<<endl;
}*/
//cout<<j.a<<","<<j.b<<":"<<i<<endl;
if(x==y){
}
else{
ans+=i;
par[x]=y;
}
}
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
219 ms |
281184 KB |
Output is correct |
2 |
Correct |
249 ms |
280616 KB |
Output is correct |
3 |
Correct |
227 ms |
281664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
257 ms |
274428 KB |
Output is correct |
2 |
Correct |
508 ms |
276008 KB |
Output is correct |
3 |
Correct |
198 ms |
281796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
203 ms |
281688 KB |
Output is correct |
2 |
Correct |
209 ms |
277900 KB |
Output is correct |
3 |
Correct |
214 ms |
281716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
391 ms |
289468 KB |
Output is correct |
2 |
Correct |
684 ms |
316360 KB |
Output is correct |
3 |
Correct |
390 ms |
294596 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
224 ms |
281028 KB |
Output is correct |
2 |
Correct |
526 ms |
300500 KB |
Output is correct |
3 |
Correct |
431 ms |
286872 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
748 ms |
301544 KB |
Output is correct |
2 |
Correct |
812 ms |
331256 KB |
Output is correct |
3 |
Correct |
393 ms |
292848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
367 ms |
278696 KB |
Output is correct |
2 |
Correct |
855 ms |
325936 KB |
Output is correct |
3 |
Correct |
377 ms |
292604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
335 ms |
335968 KB |
Output is correct |
2 |
Correct |
1488 ms |
531528 KB |
Output is correct |
3 |
Correct |
351 ms |
338496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
330 ms |
336328 KB |
Output is correct |
2 |
Correct |
2070 ms |
622760 KB |
Output is correct |
3 |
Correct |
378 ms |
342776 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
231 ms |
320580 KB |
Output is correct |
2 |
Correct |
1952 ms |
610432 KB |
Output is correct |
3 |
Correct |
401 ms |
294276 KB |
Output is correct |