#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=1e5+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[int(1e7+1)];
void solve(){
int n;
cin>>n;
int a[n];
vector<bool>check(int(1e7+1));
unordered_map<int,int>trf;
for(int i=0;i<n;i++){
cin>>a[i];
check[a[i]]=1;
parent[i]=i;
trf[a[i]]=i;
}
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]){
if(past[j]!=past[j-a[i]])v.pb({past[j]%a[i],a[i],past[j]});
}
}
sort(v.data(),v.data()+v.size(),[](edge&e1,edge&e2)-> bool {
return e1.cost<e2.cost;
});
int ans=0;
for(edge&e:v){
if(unite(trf[e.x],trf[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 |
21 ms |
40964 KB |
Output is correct |
2 |
Correct |
59 ms |
43972 KB |
Output is correct |
3 |
Correct |
21 ms |
41048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3932 KB |
Output is correct |
2 |
Correct |
247 ms |
41740 KB |
Output is correct |
3 |
Correct |
22 ms |
41044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
22 ms |
41048 KB |
Output is correct |
2 |
Correct |
21 ms |
40796 KB |
Output is correct |
3 |
Correct |
21 ms |
40944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
26304 KB |
Output is correct |
2 |
Correct |
409 ms |
65004 KB |
Output is correct |
3 |
Correct |
178 ms |
38608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
13004 KB |
Output is correct |
2 |
Correct |
208 ms |
60340 KB |
Output is correct |
3 |
Correct |
129 ms |
22492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
255 ms |
64648 KB |
Output is correct |
2 |
Correct |
476 ms |
113612 KB |
Output is correct |
3 |
Correct |
160 ms |
39284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
43 ms |
15204 KB |
Output is correct |
2 |
Correct |
499 ms |
112052 KB |
Output is correct |
3 |
Correct |
165 ms |
37972 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
73468 KB |
Output is correct |
2 |
Correct |
2560 ms |
440940 KB |
Output is correct |
3 |
Correct |
235 ms |
72136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
195 ms |
71600 KB |
Output is correct |
2 |
Correct |
4066 ms |
439200 KB |
Output is correct |
3 |
Correct |
269 ms |
72092 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
50 ms |
44752 KB |
Output is correct |
2 |
Correct |
4193 ms |
440272 KB |
Output is correct |
3 |
Correct |
170 ms |
38328 KB |
Output is correct |