#include <bits/stdc++.h>
#define int ll
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define rng(i,c,n) for(int i=c;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define vec(...) vector<__VA_ARGS__>
#define _3PGDklf ios::sync_with_stdio(0),cin.tie(0)
typedef long long ll;
using pii=pair<int,int>;
using vi=vector<int>;
void print(){cout<<'\n';}
template<class h,class...t>
void print(const h&v,const t&...u){cout<<v<<' ',print(u...);}
// e
struct dsu{
int _n;
vi si,par,leb;
dsu(int n=0){
init(n);
}
void init(int n=0){
_n=n;
par=vi(_n,0);
si=vi(_n,0);
leb=vi(_n,-1);
rep(i,n){
si[i]=1;
par[i]=i;
}
}
int parent(int u){return par[u]=(par[u]==u?u:parent(par[u]));}
bool same(int u,int v){return parent(u)==parent(v);}
void merge(int u,int v){
u=parent(u),v=parent(v);
if(!same(u,v)){
if(si[u]<si[v]) swap(u,v);
leb[u]=max(leb[u],leb[v]);
_n-=1;
si[u]+=si[v];
par[v]=u;
}
}
int size(int v=-1){return v==-1?_n:si[parent(v)];}
};
const int _n=10000100;
int nxt[_n],usd[_n];
signed main(){
_3PGDklf;
int n;
cin>>n;
vi a(n);
rep(i,n){
cin>>a[i];
}
sort(a.begin(), a.end());
a.erase(unique(a.begin(), a.end()),a.end());
if(a[0]==1){
print(0);
return 0;
}
n=sz(a);
rep(i,n){
usd[a[i]]=i+1;
}
per(i,_n-1){
if(usd[i]){
nxt[i]=usd[i];
}else{
nxt[i]=nxt[i+1];
}
}
using T=pair<int,pii>;
vec(T) es;
for(int i=2;i<_n;i++){
if(!usd[i]) continue;
for(int j=0;j<_n;j+=i){
int nj=j;
if(j==i){
nj++;
}
int id=nxt[nj];
if(id){
id-=1;
es.pb(T(a[id]%i,pii(usd[i]-1,id)));
}
}
}
sort(es.begin(), es.end());
dsu uf(n);
int ans=0;
for(auto&tp:es){
int u=tp.se.fi,v=tp.se.se;
if(!uf.same(u,v)){
ans+=tp.fi;
uf.merge(u,v);
}
}
rep(i,n){
assert(uf.same(i,0));
}
print(ans);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
82636 KB |
Output is correct |
2 |
Correct |
411 ms |
179200 KB |
Output is correct |
3 |
Correct |
71 ms |
83528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
119 ms |
79236 KB |
Output is correct |
2 |
Runtime error |
815 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
72 ms |
82956 KB |
Output is correct |
2 |
Correct |
72 ms |
81712 KB |
Output is correct |
3 |
Correct |
77 ms |
83152 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
409 ms |
137292 KB |
Output is correct |
2 |
Correct |
1195 ms |
285000 KB |
Output is correct |
3 |
Correct |
576 ms |
186660 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
135 ms |
92988 KB |
Output is correct |
2 |
Correct |
737 ms |
182896 KB |
Output is correct |
3 |
Correct |
462 ms |
133288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
748 ms |
186580 KB |
Output is correct |
2 |
Correct |
1529 ms |
285036 KB |
Output is correct |
3 |
Correct |
515 ms |
137720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
307 ms |
93280 KB |
Output is correct |
2 |
Correct |
1579 ms |
285020 KB |
Output is correct |
3 |
Correct |
536 ms |
186592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
365 ms |
207308 KB |
Output is correct |
2 |
Runtime error |
677 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
498 ms |
207388 KB |
Output is correct |
2 |
Runtime error |
690 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
127 ms |
135356 KB |
Output is correct |
2 |
Runtime error |
761 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |