#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;
int v[n];
int mx = 0;
fore(i , n)
{
cin>>v[i];
mx = max(mx , v[i]);
}
// if(n <= 1000)
// {
// vector<tuple<int , int , int>> edg;
// fore(i , n)
// {
// forr(j, i + 1, n - 1)
// {
// edg.pb({min(v[i]%v[j] , v[j]%v[i]) , i , j});
// }
// }
// sort(all(edg));
// DSU dsu(n);
// int ans =0;
// int cnt = n - 1;
// for(auto [c , U , V] : edg)
// {
// if(cnt == 0)
// break;
// if(dsu.unite(U , V))
// {
// cnt--;
// ans+=c;
// }
// }
// cout<<ans<<nl;
// }
// else
// {
sort(v , v + n);
vii edg[mx + 1];
fore(i , n)
{
int x = v[i];
int idx = i + 1;
for(int j = x;j<=mx;j+=x)
{
while(idx < n && v[idx] < j)
{
idx++;
}
if(idx < n)
{
edg[v[idx]%x].pb({i , idx});
}
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 |
163 ms |
235080 KB |
Output is correct |
2 |
Correct |
165 ms |
237396 KB |
Output is correct |
3 |
Correct |
154 ms |
234384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
220 ms |
235692 KB |
Output is correct |
3 |
Correct |
149 ms |
235348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
235088 KB |
Output is correct |
2 |
Correct |
155 ms |
234620 KB |
Output is correct |
3 |
Correct |
156 ms |
235396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1184 ms |
33888 KB |
Output is correct |
2 |
Correct |
1171 ms |
76280 KB |
Output is correct |
3 |
Correct |
1246 ms |
47876 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
25688 KB |
Output is correct |
2 |
Correct |
2174 ms |
315552 KB |
Output is correct |
3 |
Correct |
1060 ms |
25624 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1335 ms |
48416 KB |
Output is correct |
2 |
Correct |
1412 ms |
111936 KB |
Output is correct |
3 |
Correct |
1221 ms |
38708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
9040 KB |
Output is correct |
2 |
Correct |
1867 ms |
96828 KB |
Output is correct |
3 |
Correct |
1243 ms |
43360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1338 ms |
249100 KB |
Output is correct |
2 |
Correct |
2130 ms |
579480 KB |
Output is correct |
3 |
Correct |
1334 ms |
252440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1319 ms |
248588 KB |
Output is correct |
2 |
Correct |
2778 ms |
685612 KB |
Output is correct |
3 |
Correct |
1419 ms |
260692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
218 ms |
237640 KB |
Output is correct |
2 |
Correct |
2842 ms |
718856 KB |
Output is correct |
3 |
Correct |
1227 ms |
49208 KB |
Output is correct |