#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template<class T> using oset =
tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define ll long long
#define ld long double
#define ar array
#define vi vector<int>
#define vii vector<vector<int>>
#define pii pair<int, int>
#define pb push_back
#define all(x) x.begin(), x.end()
#define f first
#define s second
#define endl "\n"
const int MOD = 1e9+7;
const int inf = 1e9;
const ll linf = 1e18;
const int d4i[4]={-1, 0, 1, 0}, d4j[4]={0, 1, 0, -1};
const int d8i[8]={-1, -1, 0, 1, 1, 1, 0, -1}, d8j[8]={0, 1, 1, 1, 0, -1, -1, -1};
// -------------------------------------------------- Main Code --------------------------------------------------
const int N = 1e5+5;
int par[N], sz[N];
void make(int n) {for (int i = 0; i < n; i++) par[i] = i, sz[i] = 1;}
int find(int v) {if (par[v] == v) return v; return par[v] = find(par[v]);}
bool Union(int a, int b) {
a = find(a); b = find(b);
if (a == b) return false;
if (sz[a] < sz[b]) swap(a, b);
par[b] = a;
sz[a] += sz[b];
return true;
}
const int M = 1e7+5;
vector<array<int, 2>> edges[M];
void sol() {
int n; cin >> n;
vi arr(n);
for (int i = 0; i < n; i++) cin >> arr[i];
sort (all(arr)); arr.erase(unique(all(arr)), arr.end());
n = arr.size();
vi next(M, -1);
for (int i = 0; i < n; i++) next[arr[i]] = i;
for (int i = M-2; i >= 0; i--) if (next[i] == -1) next[i] = next[i+1];
for (int i = 0; i < n; i++) {
for (int j = arr[i]; j < M; j += arr[i]) {
int idx = next[j+(j==arr[i])];
if (idx == -1) continue;
int dist = min(arr[idx]%arr[i], arr[i]%arr[idx]);
edges[dist].pb({i, idx});
}
}
ll ans = 0;
for (int i = 0; i < M; i++) {
for (auto j : edges[i]) {
if (Union(j[0], j[1])) ans += i;
}
}
cout << ans << endl;
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
make(N);
int t = 1;
// cin >> t;
while (t--) {
sol();
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
158 ms |
275196 KB |
Output is correct |
2 |
Correct |
267 ms |
304548 KB |
Output is correct |
3 |
Correct |
167 ms |
275452 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
214 ms |
275128 KB |
Output is correct |
2 |
Correct |
1745 ms |
669920 KB |
Output is correct |
3 |
Correct |
165 ms |
275852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
275200 KB |
Output is correct |
2 |
Correct |
163 ms |
275020 KB |
Output is correct |
3 |
Correct |
187 ms |
275208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
337 ms |
285048 KB |
Output is correct |
2 |
Correct |
697 ms |
313236 KB |
Output is correct |
3 |
Correct |
389 ms |
295988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
195 ms |
277012 KB |
Output is correct |
2 |
Correct |
597 ms |
298164 KB |
Output is correct |
3 |
Correct |
384 ms |
285140 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
540 ms |
297200 KB |
Output is correct |
2 |
Correct |
903 ms |
330668 KB |
Output is correct |
3 |
Correct |
539 ms |
294048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
323 ms |
278548 KB |
Output is correct |
2 |
Correct |
968 ms |
331700 KB |
Output is correct |
3 |
Correct |
377 ms |
295804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
266 ms |
288492 KB |
Output is correct |
2 |
Correct |
1943 ms |
634332 KB |
Output is correct |
3 |
Correct |
302 ms |
291800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
323 ms |
292672 KB |
Output is correct |
2 |
Correct |
3844 ms |
747548 KB |
Output is correct |
3 |
Correct |
544 ms |
349192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
183 ms |
277384 KB |
Output is correct |
2 |
Correct |
3788 ms |
634992 KB |
Output is correct |
3 |
Correct |
416 ms |
298556 KB |
Output is correct |