#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
//#define int long long
#define vi vector<int>
#define vl vector<long long>
#define vii vector<pair<int,int>>
#define vll vector<pair<long long,long long>>
#define pb push_back
#define ll long long
#define ld long double
#define nl '\n'
#define boost ios::sync_with_stdio(false)
#define mp make_pair
#define se second
#define fi first
#define fore(i, y) for(int i = 0; i < y; i++)
#define forr(i,x,y) for(int i = x;i<=y;i++)
#define forn(i,y,x) for(int i = y; i >= x; i--)
#define all(v) v.begin(),v.end()
#define sz(v) (int)v.size()
#define clr(v,k) memset(v,k,sizeof(v))
#define rall(v) v.rbegin() , v.rend()
#define pii pair<int,int>
#define pll pair<ll , ll>
const ll MOD = 1e9 + 7;
const ll INF = 1e18 + 1;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (gcd)
ll lcm(ll a , ll b) {return a * (b / gcd(a , b));} // least common multiple (lcm)
// HERE IS THE SOLUTION
struct DSU
{
vi e;
DSU(int n)
{
e.assign(n , -1);
}
int getPar(int x)
{
return (e[x] < 0 ? x : e[x] = getPar(e[x]));
}
int getSZ(int x)
{
return -e[getPar(x)];
}
bool unite(int u , int v)
{
u = getPar(u) , v = getPar(v);
if(u == v)
return 0;
if(e[u] > e[v])
swap(u , v);
e[u]+=e[v];
e[v] = u;
return 1;
}
};
signed main()
{
boost;
cin.tie(0);
cout.tie(0);
int n ;
cin>>n;
vi v(n);
int mx = 0;
fore(i , n)
{
cin>>v[i];
mx = max(mx , v[i]);
}
sort(all(v));
vector<vii> edg(mx + 1);
fore(i , n - 1)
{
if(v[i] == v[i + 1])
{
edg[0].pb({i , i + 1});
}
}
vector<bool> vis(mx + 1 , 0);
fore(i , n)
{
if(vis[v[i]])
continue;
vis[v[i]] = 1;
int x = v[i];
for(int j = x;j<=mx;j+=x)
{
int idx ;
if(j == x)
idx = upper_bound(all(v) , j) - v.begin();
else
idx = lower_bound(all(v) , j) - v.begin();
if(idx < n && idx >= 0)
{
edg[v[idx]%x].pb({i, idx});
}
}
}
DSU dsu(n);
int ans = 0;
int cnt = n - 1;
fore(i , mx + 1)
{
if(!cnt)
break;
for(auto [a , b] : edg[i])
{
if(!cnt)
break;
if(dsu.unite(a , b))
{
ans+=i;
cnt--;
}
}
}
cout<<ans<<nl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
236460 KB |
Output is correct |
2 |
Correct |
113 ms |
264660 KB |
Output is correct |
3 |
Correct |
48 ms |
235892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
868 ms |
646828 KB |
Output is correct |
3 |
Correct |
49 ms |
237136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
48 ms |
236376 KB |
Output is correct |
2 |
Correct |
46 ms |
235860 KB |
Output is correct |
3 |
Correct |
48 ms |
236624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
107 ms |
34276 KB |
Output is correct |
2 |
Correct |
337 ms |
66132 KB |
Output is correct |
3 |
Correct |
157 ms |
47184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
19 ms |
25944 KB |
Output is correct |
2 |
Correct |
145 ms |
48332 KB |
Output is correct |
3 |
Correct |
102 ms |
22508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
225 ms |
46212 KB |
Output is correct |
2 |
Correct |
455 ms |
84024 KB |
Output is correct |
3 |
Correct |
144 ms |
43044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
9020 KB |
Output is correct |
2 |
Correct |
410 ms |
82596 KB |
Output is correct |
3 |
Correct |
170 ms |
45748 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
175 ms |
250048 KB |
Output is correct |
2 |
Correct |
2137 ms |
624688 KB |
Output is correct |
3 |
Correct |
196 ms |
252488 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
253984 KB |
Output is correct |
2 |
Correct |
3043 ms |
726976 KB |
Output is correct |
3 |
Correct |
368 ms |
311876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
64 ms |
238672 KB |
Output is correct |
2 |
Correct |
2540 ms |
598520 KB |
Output is correct |
3 |
Correct |
161 ms |
48516 KB |
Output is correct |