//#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'
llo n;
bool vis[10000001];
llo ind[10000001];
llo re[10000001+1];
llo par[100001];
llo find(llo no){
if(par[no]==no){
return no;
}
par[no]=find(par[no]);
return par[no];
}
vector<pair<llo,llo>> pre[10000001];
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin>>n;
llo ind2=0;
vector<llo> it;
for(llo i=0;i<n;i++){
llo x;
cin>>x;
if(vis[x]==0){
vis[x]=1;
ind[x]=ind2;
it.pb(x);
//cout<<x<<":"<<ind2<<endl;
ind2++;
}
}
for(llo 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(llo i=10000000;i>=1;i--){
re[i]=re[i+1];
if(vis[i]==1){
re[i]=i;
}
}
for(llo i=1;i<=10000000-1;i++){
if(vis[i]==1){
for(llo 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;
}
}
pre[re[j]-j].pb({ind[re[j]],ind[i]});
}
}
}
}
llo ans=0;
for(llo i=0;i<=10000000;i++){
for(auto j:pre[i]){
llo x=find(j.a);
llo 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 |
210 ms |
320496 KB |
Output is correct |
2 |
Correct |
551 ms |
380772 KB |
Output is correct |
3 |
Correct |
218 ms |
321348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
281 ms |
313848 KB |
Output is correct |
2 |
Runtime error |
1330 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
228 ms |
321032 KB |
Output is correct |
2 |
Correct |
219 ms |
317688 KB |
Output is correct |
3 |
Correct |
211 ms |
321092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
405 ms |
343768 KB |
Output is correct |
2 |
Correct |
782 ms |
402768 KB |
Output is correct |
3 |
Correct |
470 ms |
367816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
243 ms |
326212 KB |
Output is correct |
2 |
Correct |
578 ms |
364676 KB |
Output is correct |
3 |
Correct |
486 ms |
339288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
628 ms |
368260 KB |
Output is correct |
2 |
Correct |
967 ms |
439776 KB |
Output is correct |
3 |
Correct |
499 ms |
361348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
416 ms |
322296 KB |
Output is correct |
2 |
Correct |
1037 ms |
444944 KB |
Output is correct |
3 |
Correct |
468 ms |
366768 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
393 ms |
429096 KB |
Output is correct |
2 |
Runtime error |
988 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
423 ms |
439028 KB |
Output is correct |
2 |
Runtime error |
907 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
378160 KB |
Output is correct |
2 |
Runtime error |
1395 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |