#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;
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);
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[i]=i;
#define tup tuple<int,int,int>
priority_queue<tup,vector<tup>,greater<tup>>q;
for(int i=0;i<n-1;i++){
int*p=upper_bound(a,a+n,a[i]);
q.push({mody(a[i],*p),i,p-a});
for(int j=2;j*a[i]<=a[n-1];j++){
p=lower_bound(a,a+n,j*a[i]);
q.push({mody(a[i],*p),i,p-a});
}
}
int ans=0;
while(q.size()){
auto [cost,i,j]=q.top();
q.pop();
if(unite(i,j)){
//cerr<<a[i]<<" "<<a[j]<<"\n";
ans+=cost;
}//else cerr<<"didnt "<<a[i]<<" "<<a[j]<<"\n";
}
cout<<ans<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
661 ms |
100460 KB |
Output is correct |
3 |
Correct |
5 ms |
1368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
796 KB |
Output is correct |
2 |
Runtime error |
409 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
322 ms |
53156 KB |
Output is correct |
2 |
Correct |
1517 ms |
102044 KB |
Output is correct |
3 |
Correct |
617 ms |
102300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
8140 KB |
Output is correct |
2 |
Correct |
731 ms |
101404 KB |
Output is correct |
3 |
Correct |
314 ms |
53412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
801 ms |
101512 KB |
Output is correct |
2 |
Correct |
2073 ms |
201360 KB |
Output is correct |
3 |
Correct |
542 ms |
52388 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
15292 KB |
Output is correct |
2 |
Correct |
2229 ms |
200236 KB |
Output is correct |
3 |
Correct |
559 ms |
51960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
435 ms |
53412 KB |
Output is correct |
2 |
Runtime error |
692 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
555 ms |
53708 KB |
Output is correct |
2 |
Runtime error |
656 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
8652 KB |
Output is correct |
2 |
Runtime error |
1042 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |