// #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, res;
vi a(sz, 0);
vii g[10000005];
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;
g[a[ind] % a[i]].push_back({a[ind], a[i]});
j = a[ind] / a[i] * a[i];
}
}
DSU t(a[n]);
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();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
75 ms |
274760 KB |
Output is correct |
2 |
Correct |
83 ms |
277064 KB |
Output is correct |
3 |
Correct |
84 ms |
274904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
235600 KB |
Output is correct |
2 |
Correct |
87 ms |
275288 KB |
Output is correct |
3 |
Correct |
89 ms |
274988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
105 ms |
274796 KB |
Output is correct |
2 |
Correct |
91 ms |
274760 KB |
Output is correct |
3 |
Correct |
90 ms |
274760 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
185 ms |
248696 KB |
Output is correct |
2 |
Correct |
392 ms |
276028 KB |
Output is correct |
3 |
Correct |
189 ms |
254268 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
241140 KB |
Output is correct |
2 |
Correct |
176 ms |
261196 KB |
Output is correct |
3 |
Correct |
173 ms |
247120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
296 ms |
261100 KB |
Output is correct |
2 |
Correct |
481 ms |
287668 KB |
Output is correct |
3 |
Correct |
182 ms |
252844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
103 ms |
239636 KB |
Output is correct |
2 |
Correct |
412 ms |
285476 KB |
Output is correct |
3 |
Correct |
184 ms |
252476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
210 ms |
286536 KB |
Output is correct |
2 |
Correct |
1657 ms |
483616 KB |
Output is correct |
3 |
Correct |
232 ms |
288964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
209 ms |
286512 KB |
Output is correct |
2 |
Correct |
2409 ms |
573832 KB |
Output is correct |
3 |
Correct |
263 ms |
293856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
111 ms |
276952 KB |
Output is correct |
2 |
Correct |
2092 ms |
569500 KB |
Output is correct |
3 |
Correct |
187 ms |
255292 KB |
Output is correct |