#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
#endif
#include <bits/stdc++.h>
//#define int long long
#define pb push_back
using namespace std;
const int lim=1e7+1;
int mod=998244353;
using pii=pair<int,int>;
int parent[lim];
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;
};
int past[lim];
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);
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 |
23 ms |
79960 KB |
Output is correct |
2 |
Correct |
143 ms |
129720 KB |
Output is correct |
3 |
Correct |
25 ms |
80308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
5976 KB |
Output is correct |
2 |
Runtime error |
491 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
23 ms |
79956 KB |
Output is correct |
2 |
Correct |
22 ms |
71460 KB |
Output is correct |
3 |
Correct |
24 ms |
79956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
86 ms |
38116 KB |
Output is correct |
2 |
Correct |
246 ms |
63160 KB |
Output is correct |
3 |
Correct |
139 ms |
62904 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
15300 KB |
Output is correct |
2 |
Correct |
164 ms |
62916 KB |
Output is correct |
3 |
Correct |
77 ms |
33724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
157 ms |
62900 KB |
Output is correct |
2 |
Correct |
324 ms |
112552 KB |
Output is correct |
3 |
Correct |
115 ms |
39864 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
14540 KB |
Output is correct |
2 |
Correct |
350 ms |
113800 KB |
Output is correct |
3 |
Correct |
119 ms |
39296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
166 ms |
106152 KB |
Output is correct |
2 |
Runtime error |
537 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
107416 KB |
Output is correct |
2 |
Runtime error |
514 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
45 ms |
83184 KB |
Output is correct |
2 |
Runtime error |
666 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |