/* Author : Mychecksdead */
#include<bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
typedef long long int ll;
typedef long double ld;
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define PI 3.1415926535
#define pb push_back
#define setp() cout << setprecision(15)
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " is " << x << '\n';
const int N = 1e7+100, M = 1e5+10, F = 2147483646, K = 20;
struct Dsu {
vector<int> s, p;
Dsu(int n){
s.resize(n+1, 1);
p.resize(n+1);
for(int i = 0; i <= n; ++i) p[i] = i;
}
int find(int v){
if(p[v] == v) return v;
return (p[v] = find(p[v]));
}
void merge(int a, int b){
a = find(a);
b = find(b);
if(a != b){
if(s[a] > s[b]){
swap(a, b);
}
s[b] += s[a];
p[a] = b;
}
}
};
int n;
vector<int> v(N), a;
vector<bool> is(N);
vector<array<int, 3>> edges[N];
void solve(){
cin >> n;
a.resize(n);
for(int i = 0; i < n; ++i) cin >> a[i];
Dsu d(n);
sort(all(a));
a.resize(unique(all(a)) - a.begin());
n = a.size();
for(int i = 0; i < n - 1; ++i){
for(int j = a[i]; j <= a.back(); j += a[i]){
int k = lower_bound(a.begin() + i + 1, a.end(), j) - a.begin();
if(k >= n || a[k] - j > a[i]) continue;
edges[a[k] - j].pb({a[k] - j, k, i});
}
}
ll ans = 0;
for(int i = 0; i < N; ++i)
for(auto v: edges[i]){
if(d.find(v[1]) != d.find(v[2])){
d.merge(v[1], v[2]);
ans += v[0];
}
}
cout << ans;
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int T = 1, aa;
// cin >> T;aa=T;
while(T--){
// cout << "Case #" << aa-T << ": ";
solve();
// cout << '\n';
}
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:84:16: warning: unused variable 'aa' [-Wunused-variable]
84 | int T = 1, aa;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
148 ms |
275576 KB |
Output is correct |
2 |
Correct |
187 ms |
279236 KB |
Output is correct |
3 |
Correct |
152 ms |
275788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
275596 KB |
Output is correct |
2 |
Correct |
632 ms |
276684 KB |
Output is correct |
3 |
Correct |
155 ms |
275788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
155 ms |
275672 KB |
Output is correct |
2 |
Correct |
151 ms |
275532 KB |
Output is correct |
3 |
Correct |
155 ms |
275740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
268 ms |
290764 KB |
Output is correct |
2 |
Correct |
560 ms |
330884 KB |
Output is correct |
3 |
Correct |
318 ms |
299680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
163 ms |
278052 KB |
Output is correct |
2 |
Correct |
327 ms |
308684 KB |
Output is correct |
3 |
Correct |
264 ms |
290788 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
413 ms |
309980 KB |
Output is correct |
2 |
Correct |
685 ms |
354252 KB |
Output is correct |
3 |
Correct |
309 ms |
297128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
188 ms |
280664 KB |
Output is correct |
2 |
Correct |
645 ms |
347588 KB |
Output is correct |
3 |
Correct |
314 ms |
297372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
289 ms |
294520 KB |
Output is correct |
2 |
Correct |
2594 ms |
587404 KB |
Output is correct |
3 |
Correct |
419 ms |
298664 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
304 ms |
294392 KB |
Output is correct |
2 |
Correct |
3904 ms |
727584 KB |
Output is correct |
3 |
Correct |
530 ms |
304832 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
279132 KB |
Output is correct |
2 |
Correct |
3306 ms |
718288 KB |
Output is correct |
3 |
Correct |
335 ms |
299960 KB |
Output is correct |