#include <bits/stdc++.h>
using namespace std;
#define ios ios::sync_with_stdio(false); cin.tie(NULL);
#define pb push_back
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define fr first
#define sc second
#define mp make_pair
#define ll long long
const ll INF=1e18,N=1e7+5;
ll par[N];
vector< pair<int,int> > g[N];
int f(ll x){
if(par[x]==x) return x;
return par[x]=f(par[x]);
}
bool add(ll a, ll b){
a=f(a),b=f(b);
if(a==b) return 0;
if(rand()%2) swap(a,b);
par[a]=b;
return 1;
}
void solve(){
int n;cin>>n;
vector<ll> p(n);
for(int i=0;i<n;i++) cin>>p[i];
sort(all(p));
vector<int> nxt(N,-1);
ll mx=0;
for(int i=0;i<n;i++){
nxt[p[i]]=i;
mx=max(mx,p[i]);
par[i]=i;
}
for(int i=N-2;i>=0;i--){
if(nxt[i]==-1) nxt[i]=nxt[i+1];
}
vector<pair<int,pair<int,int> > > a;
for(int i=0;i<n-1;i++){
if(i<n-1 && p[i]==p[i+1]) continue;
g[p[i+1]%p[i]].pb(make_pair(i+1,i));
//a.pb(mp(p[i+1]%p[i],mp(p[i],p[i+1])));
for(int j=p[i]*2;j<=mx;j+=p[i]){
//a.pb(mp(nxt[j]%p[i],mp(p[i],nxt[j])));
g[p[nxt[j]]%p[i]].pb(make_pair(nxt[j],i));
}
}
sort(all(a));
ll ans=0;
for(int i=0;i<=mx;i++){
for(auto it: g[i]){
//cout<<it.fr<<' '<<it.sc<<' '<<f(it.fr)<<' '<<f(it.sc)<<' ';
if(add(it.fr,it.sc)){
ans+=i;
//cout<<"<-";
}
//cout<<endl;
}
}
cout<<ans<<endl;
}
int main(){
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
215 ms |
274608 KB |
Output is correct |
2 |
Incorrect |
197 ms |
303696 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
93 ms |
274508 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
274568 KB |
Output is correct |
2 |
Incorrect |
113 ms |
274232 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
159 ms |
285512 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
106 ms |
276320 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
182 ms |
297688 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
112 ms |
278380 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
209 ms |
288852 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
207 ms |
293096 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
154 ms |
276764 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |