#ifndef Local
#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#endif
#include <bits/stdc++.h>
//#define int long long
#define pb push_back
#define lim 100000
using namespace std;
const int mod=1000000007ll;
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);
if(x!=y){
parent[x]=y;
return 1;
}
return 0;
}
int mody(int i,int j){
return max(i,j)%min(i,j);
}
signed main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);cout.tie(NULL);
#ifdef Local
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
int n;
cin>>n;
int a[n];
for(int i=0;i<n;i++){
cin>>a[i];
}
sort(a,a+n);
n=unique(a,a+n)-a;
for(int i=0;i<n;i++)parent[a[i]]=a[i];
int now[a[n-1]+1];
memset(now,0,sizeof(now));
for(int i=0;i<n;i++){
now[a[i]]=a[i];
}
int last=-1;
for(int i=a[n-1];i;i--){
if(!now[i]){
now[i]=last;
}else last=now[i];
}
#define tup tuple<int,int,int>
priority_queue<tup,vector<tup>,greater<tup>>q;
for(int i=0;i<n-1;i++){
int nexty=now[a[i]+1];
q.push({mody(a[i],nexty),a[i],nexty});
int last=nexty;
for(int j=2;j*a[i]<=a[n-1];j++){
nexty=now[j*a[i]];
if(nexty!=last)q.push({mody(a[i],nexty),a[i],nexty});
last=nexty;
}
}
int ans=0;
while(q.size()){
auto [cost,i,j]=q.top();
q.pop();
if(unite(i,j)){
//cerr<<i<<" "<<j<<"\n";
ans+=cost;
}//else cerr<<"didnt "<<i<<" "<<j<<" "<<find(i)<<" "<<find(j)<<"\n";
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
29 ms |
39512 KB |
Output is correct |
2 |
Correct |
115 ms |
43128 KB |
Output is correct |
3 |
Correct |
32 ms |
39772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
252 ms |
40364 KB |
Output is correct |
3 |
Correct |
31 ms |
39760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
30 ms |
39612 KB |
Output is correct |
2 |
Correct |
28 ms |
39516 KB |
Output is correct |
3 |
Correct |
35 ms |
39768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
381 ms |
22628 KB |
Output is correct |
2 |
Correct |
1490 ms |
60756 KB |
Output is correct |
3 |
Correct |
510 ms |
33232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
8144 KB |
Output is correct |
2 |
Correct |
612 ms |
54836 KB |
Output is correct |
3 |
Correct |
382 ms |
19408 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
926 ms |
59300 KB |
Output is correct |
2 |
Correct |
1822 ms |
109144 KB |
Output is correct |
3 |
Correct |
456 ms |
36020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
9828 KB |
Output is correct |
2 |
Correct |
1807 ms |
106936 KB |
Output is correct |
3 |
Correct |
408 ms |
34256 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
568 ms |
70024 KB |
Output is correct |
2 |
Execution timed out |
5075 ms |
440156 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
594 ms |
71084 KB |
Output is correct |
2 |
Execution timed out |
5038 ms |
438464 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
43472 KB |
Output is correct |
2 |
Execution timed out |
5018 ms |
437912 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |