#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define ppb pop_back
#define ep insert
#define endl '\n'
#define elif else if
#define pow pwr
#define sqrt sqrtt
//#define int long long
#define ll long long
#define y1 YONE
typedef unsigned long long ull;
using namespace std;
const int N=1e7+5;
int a[N],n,par[N],sz[N];
int getpar(int x){
if (x==par[x]) return x;
return par[x]=getpar(par[x]);
}
void link(int x,int y){
x=getpar(x);
y=getpar(y);
if (x==y) return;
if (sz[x]>sz[y]) swap(x,y);
sz[y]+=sz[x];
par[x]=y;
return;
}
int32_t main(){
ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
cin>>n;
for (int i=1;i<=n;i++) cin>>a[i],par[i]=i,sz[i]=1;
sort(a+1,a+1+n);
vector<vector<int>> v;
for (int i=1;i<n;i++){
v.pb({a[i+1]%a[i],i,i+1});
for (int j=a[i]*2;j<N;j+=i){
int l=1,r=n,mid,idx=-1;
while (l<=r){
mid=(l+r)/2;
if (a[mid]>=j){
idx=mid;
r=mid-1;
}else l=mid+1;
}
if (idx==-1) break;
if (a[idx]>=j+i) continue;
v.pb({a[idx]%a[i],i,idx});
}
}int ans=0;
sort(v.begin(),v.end());
for (auto u:v){
int x=getpar(u[1]),y=getpar(u[2]);
if (x==y) continue;
ans+=u[0];
link(x,y);
}cout<<ans<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
418 ms |
14400 KB |
Output is correct |
2 |
Correct |
487 ms |
23024 KB |
Output is correct |
3 |
Correct |
428 ms |
22952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
7 ms |
4880 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
400 ms |
13808 KB |
Output is correct |
2 |
Correct |
188 ms |
1084 KB |
Output is correct |
3 |
Correct |
427 ms |
14508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3878 ms |
448876 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1457 ms |
230188 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
4257 ms |
658256 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
569 ms |
89488 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1887 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1864 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
1880 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |