#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define pf push_front
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define EPSILON 0.000001
using namespace std;
const ll NMAX = 1e7+5, INF = 1e18, MOD = 1e9+7, MMAX = 1e2 + 5, inf = INT_MAX;
ifstream fin("date.in");
ofstream fout("aleatoare.out");
int N,maxi;
int par[NMAX],siz[NMAX];
set<int> S;
ll ans;
struct edge
{
int a,b,cost;
};
vector<edge> edges;
int parent(int node)
{
if(par[node]==node)return node;
return(par[node]=parent(par[node]));
}
void dsu(int a,int b,int cost)
{
a=parent(a);
b=parent(b);
if(a!=b)
{
if(siz[a]<siz[b])swap(a,b);
siz[a]+=siz[b];
par[b]=a;
ans+=cost;
}
}
bool cmp(edge a, edge b)
{
return a.cost<b.cost;
}
void kruskal()
{
sort(edges.begin(),edges.end(),cmp);
for(auto x : edges)
{
int a=x.a;
int b=x.b;
int cost=x.cost;
dsu(a,b,cost);
}
}
int main()
{
cin.tie ( 0 )->sync_with_stdio ( 0 );
cin.tie ( NULL );
cout.tie ( NULL );
cin>>N;
for(int i=1;i<=N;i++)
{
int x;
cin>>x;
S.insert(x);
par[x]=x;
siz[x]=1;
maxi=max(maxi,x);
}
for(auto x : S)
{
auto it=S.upper_bound(x);
int a=x;
if(it!=S.end())
{
int b=*it;
int cost=*it-x;
edges.pb({a,b,cost});
}
for(int j=2*x;j<=maxi;j+=x)
{
it=S.lower_bound(j);
if(it!=S.end())
{
int b=*it;
int cost=*it-j;
edges.pb({a,b,cost});
}
}
}
kruskal();
cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
7764 KB |
Output is correct |
2 |
Correct |
362 ms |
53608 KB |
Output is correct |
3 |
Correct |
7 ms |
8596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
596 KB |
Output is correct |
2 |
Runtime error |
1004 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
8276 KB |
Output is correct |
2 |
Correct |
3 ms |
5332 KB |
Output is correct |
3 |
Correct |
5 ms |
8468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
366 ms |
37748 KB |
Output is correct |
2 |
Correct |
914 ms |
61864 KB |
Output is correct |
3 |
Correct |
432 ms |
61736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
12120 KB |
Output is correct |
2 |
Correct |
373 ms |
56744 KB |
Output is correct |
3 |
Correct |
278 ms |
33252 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
608 ms |
62228 KB |
Output is correct |
2 |
Correct |
1259 ms |
111272 KB |
Output is correct |
3 |
Correct |
395 ms |
37328 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
85 ms |
10040 KB |
Output is correct |
2 |
Correct |
1130 ms |
110380 KB |
Output is correct |
3 |
Correct |
388 ms |
37536 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
108224 KB |
Output is correct |
2 |
Runtime error |
2390 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
465 ms |
108340 KB |
Output is correct |
2 |
Runtime error |
2025 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
72896 KB |
Output is correct |
2 |
Runtime error |
3207 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |