#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 nxt[N],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));
ll mx=0;
for(int i=0;i<n;i++){
nxt[p[i]]=p[i];
mx=max(mx,p[i]);
par[p[i]]=p[i];
}
for(int i=mx;i>=0;i--){
if(nxt[i]==0) nxt[i]=nxt[i+1];
}
vector<pair<int,pair<int,int> > > a;
for(int i=0;i<n;i++){
if(i<n-1 && p[i]==p[i+1]) continue;
g[p[i+1]%p[i]].pb(make_pair(p[i+1],p[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[nxt[j]%p[i]].pb(make_pair(nxt[j],p[i]));
}
}
sort(all(a));
ll ans=0;
for(int i=0;i<=mx;i++){
for(auto it: g[i])
if(add(it.fr,it.sc)) ans+=i;
}
cout<<ans<<endl;
}
int main(){
solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
156 ms |
317268 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
235348 KB |
Output is correct |
2 |
Correct |
1064 ms |
708740 KB |
Output is correct |
3 |
Correct |
158 ms |
318288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
159 ms |
317520 KB |
Output is correct |
2 |
Incorrect |
141 ms |
316344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
131 ms |
261200 KB |
Output is correct |
2 |
Correct |
205 ms |
289644 KB |
Output is correct |
3 |
Correct |
144 ms |
272312 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
78 ms |
252920 KB |
Output is correct |
2 |
Correct |
147 ms |
270780 KB |
Output is correct |
3 |
Correct |
115 ms |
253612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
152 ms |
273312 KB |
Output is correct |
2 |
Correct |
218 ms |
307212 KB |
Output is correct |
3 |
Correct |
137 ms |
270564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
77 ms |
242084 KB |
Output is correct |
2 |
Correct |
220 ms |
307944 KB |
Output is correct |
3 |
Correct |
157 ms |
272056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
270 ms |
404820 KB |
Output is correct |
2 |
Correct |
1502 ms |
750236 KB |
Output is correct |
3 |
Correct |
300 ms |
407696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
270 ms |
409336 KB |
Output is correct |
2 |
Runtime error |
1303 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
171 ms |
365908 KB |
Output is correct |
2 |
Correct |
1733 ms |
716184 KB |
Output is correct |
3 |
Correct |
143 ms |
274292 KB |
Output is correct |