#include<bits/stdc++.h>
using namespace std;
#define task "CC"
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define bit(n,i) ((n>>i)&1)
#define all(x) x.begin(),x.end()
//#pragma GCC optimize("Ofast,unroll-loops")
//#pragma GCC target("avx,avx2,bmi,bmi2,sse,sse2,sse3,ssse3,sse4,popcnt")
//#define int long long
typedef long long ll;
typedef pair<int,int> pii;
typedef double ld;
typedef pair<ld,ld> pdd;
typedef pair<ll,ll> pll;
const ll maxn=1e7+1;
const ll N=1e5+2;
int n,nxt[maxn+1],par[N],x[N];
bool ct[N];
vector<pii> Edge[maxn];
int Find(int v) {
return (par[v] < 0) ?v: (par[v] = Find(par[v]));
}
bool Union(int x, int y) {
if ((x = Find(x)) == (y = Find(y)))
{
return false ;
}
if (par[y] < par[x])
{
swap(x, y);
}
par[x] += par[y];
par[y] = x;
return true;
}
void sol()
{
cin >> n;
for (int i=1;i<=n;i++)
{
cin >> x[i];
par[i]=-1;
}
sort(x+1,x+1+n);
for1(i,1,n)
{
if (nxt[x[i]]==0) nxt[x[i]]=i;
}
int ans=0;
nxt[(int)x[n]+1]=-1;
for2(i,x[n],1)
{
if (nxt[i]==0) nxt[i]=nxt[i+1];
//cerr<< i<< ' '<<nxt[i]<<'\n';
}
// for (int i=1;i<=x[n];i++)
// {
// //cerr<<"wtf\n";
// if (nxt[i]==i)
// {
// if (nxt[i+1]==-1) continue;
// Edge[nxt[i+1]-i].pb({nxt[i+1],i});
// for (int j=2*i;j<=x[n];j+=i)
// {
// if (nxt[j]==-1) break;
// Edge[nxt[j]-j].pb({nxt[j],i});
// }
// }
// }
for1(k,1,n)
{
if (x[k]==x[k-1]) continue;
int i=x[k];
if (nxt[i+1]==-1) continue;
Edge[x[nxt[i+1]]-i].pb({nxt[i+1],k});
if (ct[k]) continue;
for (int j=2*i;j<=x[n];j+=i)
{
if (nxt[j]==-1) break;
Edge[x[nxt[j]]-j].pb({nxt[j],k});
if (x[nxt[j]]==j) ct[nxt[j]]=1;
}
}
for (int i=0;i<=x[n];i++)
{
for (auto x:Edge[i])
{
//cerr<<i<<' '<<x.se<<' '<<x.fi<<'\n';
if (!Union(x.fi,x.se)) continue;
ans+=i;
}
Edge[i].clear();
}
cout << ans;
}
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
sol();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
275280 KB |
Output is correct |
2 |
Correct |
275 ms |
307832 KB |
Output is correct |
3 |
Correct |
110 ms |
275536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
50 ms |
236624 KB |
Output is correct |
2 |
Correct |
975 ms |
504116 KB |
Output is correct |
3 |
Correct |
110 ms |
276304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
106 ms |
275248 KB |
Output is correct |
2 |
Correct |
105 ms |
275288 KB |
Output is correct |
3 |
Correct |
106 ms |
275536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
83 ms |
247732 KB |
Output is correct |
2 |
Correct |
97 ms |
256500 KB |
Output is correct |
3 |
Correct |
104 ms |
259412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
59 ms |
242000 KB |
Output is correct |
2 |
Correct |
91 ms |
253856 KB |
Output is correct |
3 |
Correct |
77 ms |
245472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
252932 KB |
Output is correct |
2 |
Correct |
107 ms |
259980 KB |
Output is correct |
3 |
Correct |
88 ms |
252736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
60 ms |
239632 KB |
Output is correct |
2 |
Correct |
101 ms |
260404 KB |
Output is correct |
3 |
Correct |
96 ms |
258972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
161 ms |
288852 KB |
Output is correct |
2 |
Correct |
478 ms |
419584 KB |
Output is correct |
3 |
Correct |
170 ms |
291788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
174 ms |
292712 KB |
Output is correct |
2 |
Correct |
613 ms |
465268 KB |
Output is correct |
3 |
Correct |
273 ms |
345224 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
114 ms |
277584 KB |
Output is correct |
2 |
Correct |
660 ms |
433036 KB |
Output is correct |
3 |
Correct |
98 ms |
258448 KB |
Output is correct |