// #pragma GCC optimize("O3,unroll-loops")
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vi vector<int>
#define vl vector<ll>
#define vii vector<pii>
#define db long double
#define vll vector<pll>
#define endl '\n'
#define all(x) x.begin(), x.end()
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
// #define int long long
struct DSU{
vi par;
int components;
DSU(int sz){
components = sz;
par.resize(sz + 5, -1);
}
int Find(int u){
if(par[u] < 0) return u;
else return par[u] = Find(par[u]);
}
bool Union(int u, int v){
u = Find(u), v = Find(v);
if(u != v){
if(par[u] < par[v]){
swap(u, v);
}
par[u] += par[v];
par[v] = u;
components--;
return true;
}
else{
return false;
}
}
};
const int sz = 1e5 + 5;
int n, a[sz], mx;
ll res = 0;
void fmain(){
fastio;
cin >> n;
set<int> st;
for(int i = 1; i <= n; i++){
cin >> a[i];
mx = max(mx, a[i]);
st.insert(a[i]);
}
vector<pair<int, pii>> v;
for(int i : st){
auto itt = st.upper_bound(i);
int j = i * 2;
if(itt != st.end()) v.push_back({min(*itt % i, i % *itt), {i, *itt}}), j = *itt / i * i;
for(; j <= mx; j += i){
auto it = st.lower_bound(j);
if(it != st.end()){
v.push_back({min(*it % i, i % *it), {i, *it}});
j = *it / i * i;
}
}
}
sort(all(v));
DSU t(mx);
for(auto i : v){
res += i.first * t.Union(i.second.first, i.second.second);
}
cout << res;
}
signed main(){
int tmr = 1;
// cin >> tmr;
while(tmr--){
fmain();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
39760 KB |
Output is correct |
2 |
Correct |
34 ms |
41812 KB |
Output is correct |
3 |
Correct |
10 ms |
39696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
592 KB |
Output is correct |
2 |
Correct |
14 ms |
40320 KB |
Output is correct |
3 |
Correct |
10 ms |
39696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
39760 KB |
Output is correct |
2 |
Correct |
8 ms |
39404 KB |
Output is correct |
3 |
Correct |
9 ms |
39696 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
326 ms |
32060 KB |
Output is correct |
2 |
Correct |
1044 ms |
56216 KB |
Output is correct |
3 |
Correct |
385 ms |
31144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
7200 KB |
Output is correct |
2 |
Correct |
311 ms |
52680 KB |
Output is correct |
3 |
Correct |
305 ms |
31144 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
735 ms |
56552 KB |
Output is correct |
2 |
Correct |
1371 ms |
105732 KB |
Output is correct |
3 |
Correct |
392 ms |
31908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
95 ms |
9548 KB |
Output is correct |
2 |
Correct |
1163 ms |
104900 KB |
Output is correct |
3 |
Correct |
356 ms |
31516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
372 ms |
61084 KB |
Output is correct |
2 |
Execution timed out |
5077 ms |
401632 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
352 ms |
61088 KB |
Output is correct |
2 |
Execution timed out |
5084 ms |
398788 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
54 ms |
42936 KB |
Output is correct |
2 |
Execution timed out |
5078 ms |
400272 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |