#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]){
if(past[j]!=past[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();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
26 ms |
79960 KB |
Output is correct |
2 |
Correct |
56 ms |
82824 KB |
Output is correct |
3 |
Correct |
26 ms |
79964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
5980 KB |
Output is correct |
2 |
Correct |
221 ms |
80720 KB |
Output is correct |
3 |
Correct |
25 ms |
79964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
79960 KB |
Output is correct |
2 |
Correct |
22 ms |
71436 KB |
Output is correct |
3 |
Correct |
24 ms |
79940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
25032 KB |
Output is correct |
2 |
Correct |
228 ms |
63404 KB |
Output is correct |
3 |
Correct |
96 ms |
37564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
15820 KB |
Output is correct |
2 |
Correct |
171 ms |
63672 KB |
Output is correct |
3 |
Correct |
74 ms |
21700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
63540 KB |
Output is correct |
2 |
Correct |
289 ms |
111884 KB |
Output is correct |
3 |
Correct |
93 ms |
38332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
28 ms |
16328 KB |
Output is correct |
2 |
Correct |
283 ms |
111488 KB |
Output is correct |
3 |
Correct |
91 ms |
38556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
106364 KB |
Output is correct |
2 |
Correct |
1609 ms |
476300 KB |
Output is correct |
3 |
Correct |
183 ms |
107700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
140 ms |
105816 KB |
Output is correct |
2 |
Correct |
2407 ms |
476876 KB |
Output is correct |
3 |
Correct |
191 ms |
107448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
45 ms |
84108 KB |
Output is correct |
2 |
Correct |
2512 ms |
475312 KB |
Output is correct |
3 |
Correct |
101 ms |
37816 KB |
Output is correct |