//#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;i++){
if(vis[i]==1){
for(llo j=i;j<=10000000;j+=i){
if(re[j]>=0){
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);
//cout<<x<<","<<y<<","<<j.a<<","<<j.b<<endl;
if(x==y){
}
else{
ans+=i;
par[x]=y;
}
}
}
cout<<ans<<endl;
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
305 ms |
255048 KB |
Output is correct |
2 |
Correct |
206 ms |
249068 KB |
Output is correct |
3 |
Correct |
325 ms |
257000 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
162 ms |
245760 KB |
Output is correct |
2 |
Correct |
156 ms |
241396 KB |
Output is correct |
3 |
Correct |
339 ms |
257380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
343 ms |
257604 KB |
Output is correct |
2 |
Correct |
255 ms |
250656 KB |
Output is correct |
3 |
Correct |
326 ms |
257496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3361 ms |
786436 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3531 ms |
786436 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
3336 ms |
786436 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2045 ms |
786436 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4956 ms |
786436 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
4870 ms |
786436 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
5060 ms |
786436 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |