#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;
};
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 |
50 ms |
77900 KB |
Output is correct |
2 |
Correct |
189 ms |
128516 KB |
Output is correct |
3 |
Correct |
52 ms |
78156 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
1880 KB |
Output is correct |
2 |
Runtime error |
577 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
52 ms |
78024 KB |
Output is correct |
2 |
Correct |
48 ms |
69464 KB |
Output is correct |
3 |
Correct |
51 ms |
78076 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
91 ms |
36512 KB |
Output is correct |
2 |
Correct |
241 ms |
60084 KB |
Output is correct |
3 |
Correct |
133 ms |
61356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
13008 KB |
Output is correct |
2 |
Correct |
166 ms |
60848 KB |
Output is correct |
3 |
Correct |
79 ms |
32440 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
159 ms |
61456 KB |
Output is correct |
2 |
Correct |
329 ms |
109848 KB |
Output is correct |
3 |
Correct |
118 ms |
36280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
13000 KB |
Output is correct |
2 |
Correct |
357 ms |
110072 KB |
Output is correct |
3 |
Correct |
121 ms |
35260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
194 ms |
106292 KB |
Output is correct |
2 |
Runtime error |
683 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
213 ms |
105272 KB |
Output is correct |
2 |
Runtime error |
644 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
83676 KB |
Output is correct |
2 |
Runtime error |
828 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |