/*Author: KawakiMeido*/
#include <bits/stdc++.h>
#define pb push_back
#define endl "\n"
#define ll long long
#define pii pair<int,int>
#define piii pair<int,pii>
#define fi first
#define se second
using namespace std;
/*Constants*/
const int N = 1e5+10;
const int M = 1e7+10;
const int INF = 1e9+7;
const long long LLINF = 1e18+3;
/*TestCases*/
int test=1;
void solve();
void TestCases(bool v){
if (v) cin >> test;
while(test--) solve();
}
/*Global Variables*/
int n;
vector<int> a;
vector<piii> edge;
int vis[M];
int nxt[M];
//DSU
int parent[N];
int Find(int x){
return (x == parent[x]) ? x : parent[x] = Find(parent[x]);
}
void Union(int u, int v){
int x = Find(u);
int y = Find(v);
if (x!=y){
parent[y] = x;
}
}
/*Solution*/
void solve(){
cin >> n;
int mx = 0;
int mn = INF;
int cnt = 0;
for (int i=1; i<=n; i++){
int x;
cin >> x;
if (!vis[x]){
++cnt;
vis[x] = cnt;;
a.push_back(x);
mx = max(mx,x);
mn = min(mn,x);
}
}
sort(a.begin(),a.end());
int rawr = INF;
for (int i=mx+1; i>=1; i--){
if (vis[i]) rawr = i;
nxt[i] = rawr;
}
for (int idx = 0; idx<(int)a.size(); idx++){
int x = a[idx];
for (int i=x; i<=mx; i+=x){
if (i == x){
if (nxt[i+1]<i+x){
edge.push_back({nxt[i+1]-i,{x,nxt[i+1]}});
}
}
else{
if (nxt[i]<i+x){
edge.push_back({nxt[i]-i,{x,nxt[i]}});
}
}
}
}
for (int i=1; i<=cnt; i++){
parent[i] = i;
}
sort(edge.begin(),edge.end());
ll ans = 0;
for (auto in:edge){
int w = in.fi;
int u = vis[in.se.fi];
int v = vis[in.se.se];
// cout << in.se.fi << " " << in.se.se << " " << w << endl;
if (Find(u)!=Find(v)){
ans+=w;
Union(u,v);
}
}
cout << ans;
}
/*Driver Code*/
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
TestCases(0);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
43608 KB |
Output is correct |
2 |
Correct |
43 ms |
44752 KB |
Output is correct |
3 |
Correct |
13 ms |
43612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2652 KB |
Output is correct |
2 |
Correct |
123 ms |
41056 KB |
Output is correct |
3 |
Correct |
13 ms |
43864 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
43864 KB |
Output is correct |
2 |
Correct |
11 ms |
42168 KB |
Output is correct |
3 |
Correct |
12 ms |
43868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
23704 KB |
Output is correct |
2 |
Correct |
489 ms |
62272 KB |
Output is correct |
3 |
Correct |
192 ms |
37188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
13824 KB |
Output is correct |
2 |
Correct |
245 ms |
59968 KB |
Output is correct |
3 |
Correct |
134 ms |
20296 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
318 ms |
61096 KB |
Output is correct |
2 |
Correct |
620 ms |
110884 KB |
Output is correct |
3 |
Correct |
175 ms |
35988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
11600 KB |
Output is correct |
2 |
Correct |
591 ms |
109976 KB |
Output is correct |
3 |
Correct |
169 ms |
37704 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
106304 KB |
Output is correct |
2 |
Correct |
3125 ms |
474756 KB |
Output is correct |
3 |
Correct |
235 ms |
106244 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
196 ms |
105792 KB |
Output is correct |
2 |
Correct |
4178 ms |
473380 KB |
Output is correct |
3 |
Correct |
292 ms |
106636 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
49 ms |
77828 KB |
Output is correct |
2 |
Correct |
4016 ms |
467844 KB |
Output is correct |
3 |
Correct |
211 ms |
37712 KB |
Output is correct |