#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
#endif
#include <bits/stdc++.h>
#define int long long
#define pb push_back
using namespace std;
int lim=1e7+1;
int mod=998244353;
using pii=pair<int,int>;
unordered_map<int,int>parent;
int find(int i){
if(parent[i]==i)return i;
return parent[i]=find(parent[i]);
}
bool unite(int i,int j){
int x=find(i),y=find(j);
parent[x]=y;
return x^y;
}
struct edge{
int cost,x,y;
};
void solve(){
int n;
cin>>n;
int a[n];
vector<bool>check(lim);
for(int i=0;i<n;i++){
cin>>a[i];
parent[a[i]]=a[i];
check[a[i]]=1;
}
if(check[1]){
cout<<"0\n";
return;
}
sort(a,a+n);
int past[a[n-1]+1];
past[a[n-1]]=a[n-1];
for(int j=a[n-1]-1;j;j--){
past[j]=past[j+1];
if(check[j])past[j]=j;
}
vector<edge>v;
for(int i=0;i<n-1;i++){
if(a[i]==a[i+1])continue;
v.pb({a[i+1]%a[i],a[i],a[i+1]});
for(int j=2*a[i];j<=a[n-1];j+=a[i]){
v.pb({past[j]%a[i],a[i],past[j]});
}
}
sort(v.begin(),v.end(),[](edge&e1,edge&e2)-> bool {
return e1.cost<e2.cost;
});
int ans=0;
for(edge&e:v){
if(unite(e.x,e.y)){
ans+=e.cost;
}
}
cout<<ans<<"\n";
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
#ifdef Local
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#else
#endif
int t=1;
//cin>>t;
while (t--)
{
solve();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
61 ms |
80188 KB |
Output is correct |
2 |
Correct |
399 ms |
179828 KB |
Output is correct |
3 |
Correct |
68 ms |
80468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
2076 KB |
Output is correct |
2 |
Runtime error |
484 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
67 ms |
80228 KB |
Output is correct |
2 |
Correct |
66 ms |
79896 KB |
Output is correct |
3 |
Correct |
69 ms |
80364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
295 ms |
66484 KB |
Output is correct |
2 |
Correct |
772 ms |
115600 KB |
Output is correct |
3 |
Correct |
401 ms |
114924 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
18120 KB |
Output is correct |
2 |
Correct |
347 ms |
111476 KB |
Output is correct |
3 |
Correct |
291 ms |
60616 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
517 ms |
115572 KB |
Output is correct |
2 |
Correct |
962 ms |
213388 KB |
Output is correct |
3 |
Correct |
350 ms |
65976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
17760 KB |
Output is correct |
2 |
Correct |
994 ms |
212192 KB |
Output is correct |
3 |
Correct |
386 ms |
64456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
450 ms |
136092 KB |
Output is correct |
2 |
Runtime error |
544 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
429 ms |
136368 KB |
Output is correct |
2 |
Runtime error |
497 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
127 ms |
88160 KB |
Output is correct |
2 |
Runtime error |
590 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |