// #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;
ll res = 0;
vi a(sz, 0);
void fmain(){
fastio;
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i];
}
sort(a.begin() + 1, a.begin() + n + 1);
vector<pair<int, pii>> v;
for(int i = 1; i <= n; i++){
if(a[i] == a[i - 1]) continue;
for(int j = a[i]; j <= a[n]; j += a[i]){
int ind = (j == a[i] ? upper_bound(a.begin() + 1, a.begin() + n + 1, j) : lower_bound(a.begin() + 1, a.begin() + n + 1, j)) - a.begin();
// cout << ind << endl;
if(ind > n) break;
// cout << i << " " << j << endl;
v.push_back({a[ind] % a[i], {a[ind], a[i]}});
j = a[ind] / a[i] * a[i];
}
}
sort(all(v));
DSU t(a[n]);
for(auto i : v){
// cout << i.first << " " << i.second.first << " " << i.second.second << endl;
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 |
40016 KB |
Output is correct |
2 |
Correct |
30 ms |
42180 KB |
Output is correct |
3 |
Correct |
11 ms |
39952 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
1016 KB |
Output is correct |
2 |
Correct |
16 ms |
40660 KB |
Output is correct |
3 |
Correct |
9 ms |
40208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
40016 KB |
Output is correct |
2 |
Correct |
8 ms |
39832 KB |
Output is correct |
3 |
Correct |
9 ms |
40016 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
210 ms |
17016 KB |
Output is correct |
2 |
Correct |
789 ms |
52128 KB |
Output is correct |
3 |
Correct |
289 ms |
27608 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
30 ms |
6840 KB |
Output is correct |
2 |
Correct |
332 ms |
52128 KB |
Output is correct |
3 |
Correct |
215 ms |
15280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
488 ms |
52044 KB |
Output is correct |
2 |
Correct |
965 ms |
101364 KB |
Output is correct |
3 |
Correct |
261 ms |
27556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
71 ms |
8132 KB |
Output is correct |
2 |
Correct |
879 ms |
101272 KB |
Output is correct |
3 |
Correct |
243 ms |
27556 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
245 ms |
54428 KB |
Output is correct |
2 |
Correct |
4373 ms |
394836 KB |
Output is correct |
3 |
Correct |
291 ms |
59292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
235 ms |
54248 KB |
Output is correct |
2 |
Execution timed out |
5078 ms |
396856 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
42 ms |
42168 KB |
Output is correct |
2 |
Execution timed out |
5074 ms |
396784 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |