// #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;
vii g[sz * 100];
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]);
}
for(int i : st){
auto itt = st.upper_bound(i);
if(itt != st.end()) g[*itt % i].push_back({i, *itt});
for(int j = i * 2; j <= mx; j += i){
auto it = st.lower_bound(j);
if(it != st.end()){
g[*it % i].push_back({i, *it});
j = *it / i * i;
}
}
}
DSU t(mx);
for(int i = 0; i <= 1e7; i++){
for(pii j : g[i]){
res += t.Union(j.first, j.second) * i;
}
}
cout << res;
}
signed main(){
int tmr = 1;
// cin >> tmr;
while(tmr--){
fmain();
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
85 ms |
274560 KB |
Output is correct |
2 |
Correct |
89 ms |
276808 KB |
Output is correct |
3 |
Correct |
89 ms |
274504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
62 ms |
235444 KB |
Output is correct |
2 |
Correct |
107 ms |
275016 KB |
Output is correct |
3 |
Correct |
118 ms |
274504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
92 ms |
274504 KB |
Output is correct |
2 |
Correct |
98 ms |
274256 KB |
Output is correct |
3 |
Correct |
99 ms |
274504 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
294 ms |
253304 KB |
Output is correct |
2 |
Correct |
781 ms |
283868 KB |
Output is correct |
3 |
Correct |
315 ms |
257968 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
84 ms |
241400 KB |
Output is correct |
2 |
Correct |
185 ms |
261704 KB |
Output is correct |
3 |
Correct |
249 ms |
250884 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
517 ms |
265304 KB |
Output is correct |
2 |
Correct |
837 ms |
292080 KB |
Output is correct |
3 |
Correct |
291 ms |
257344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
122 ms |
241092 KB |
Output is correct |
2 |
Correct |
657 ms |
289016 KB |
Output is correct |
3 |
Correct |
282 ms |
256184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
327 ms |
291368 KB |
Output is correct |
2 |
Correct |
3187 ms |
488616 KB |
Output is correct |
3 |
Correct |
395 ms |
293828 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
350 ms |
291228 KB |
Output is correct |
2 |
Correct |
4430 ms |
581084 KB |
Output is correct |
3 |
Correct |
389 ms |
297292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
114 ms |
277576 KB |
Output is correct |
2 |
Correct |
3733 ms |
573444 KB |
Output is correct |
3 |
Correct |
339 ms |
258232 KB |
Output is correct |