#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);
ll mx=0;
for(int i=0;i<n;i++){
cin>>p[i];
mx=max(mx,p[i]);
}
sort(all(p));
vector<int> nxt(mx+1,-1);
for(int i=0;i<n;i++){
nxt[p[i]]=i;
par[i]=i;
}
for(int i=mx-1;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 |
123 ms |
274392 KB |
Output is correct |
2 |
Incorrect |
235 ms |
303764 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
56 ms |
235348 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
122 ms |
274380 KB |
Output is correct |
2 |
Incorrect |
117 ms |
274348 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
118 ms |
250276 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
71 ms |
241296 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
149 ms |
262444 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
76 ms |
239856 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
245 ms |
288868 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
226 ms |
293100 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
131 ms |
276948 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |