#include <bits/stdc++.h>
using namespace std;
const int mN = 1e5 + 5, mP = 1e7 + 5;
int n, mx, ub[mP], par[mP], sz[mP]; long long ans; vector<int> A; vector<pair<int, int>> edg[mP];
int get(int i){
return (i == par[i]) ? i : par[i] = get(par[i]);
}
void merge(int a, int b, int w){
a = get(a); b = get(b); if (a == b) return;
if (a > b) swap(a, b);
par[a] = b; sz[b] += sz[a]; ans += w;
}
int main(){
cin >> n; A.resize(n);
for (int &i : A) cin >> i;
sort(A.begin(), A.end()); A.erase(unique(A.begin(), A.end()), A.end());
n = A.size(); mx = A.back(); iota(par, par + n, 0); fill(sz, sz + n, 1);
for (int i = 0; i < n; i++) ub[A[i]] = i;
for (int i = mx, cur = n - 1; i >= 0; i--) (ub[i]) ? cur = ub[i] : ub[i] = cur;
for (int i = 0; i < n - 1; i++)
for (int x = A[i]; x <= mx; x += A[i])
edg[A[ub[x + (x == A[i])]] % x].push_back({i, ub[x + (x == A[i])]});
for (int i = 0; i < mx; i++)
for (auto edge : edg[i])
merge(edge.first, edge.second, i);
cout<<ans<<endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
173 ms |
274344 KB |
Output is correct |
2 |
Correct |
432 ms |
306792 KB |
Output is correct |
3 |
Correct |
173 ms |
274604 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
125 ms |
235268 KB |
Output is correct |
2 |
Runtime error |
2639 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
189 ms |
274324 KB |
Output is correct |
2 |
Correct |
193 ms |
274164 KB |
Output is correct |
3 |
Correct |
193 ms |
274428 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
239 ms |
250444 KB |
Output is correct |
2 |
Correct |
375 ms |
279896 KB |
Output is correct |
3 |
Correct |
270 ms |
262564 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
164 ms |
241276 KB |
Output is correct |
2 |
Correct |
307 ms |
263716 KB |
Output is correct |
3 |
Correct |
224 ms |
248572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
302 ms |
262972 KB |
Output is correct |
2 |
Correct |
447 ms |
298152 KB |
Output is correct |
3 |
Correct |
268 ms |
259472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
171 ms |
240008 KB |
Output is correct |
2 |
Correct |
441 ms |
301636 KB |
Output is correct |
3 |
Correct |
278 ms |
262024 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
361 ms |
289488 KB |
Output is correct |
2 |
Correct |
2292 ms |
635688 KB |
Output is correct |
3 |
Correct |
381 ms |
292500 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
393 ms |
293816 KB |
Output is correct |
2 |
Runtime error |
3519 ms |
786436 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
222 ms |
276804 KB |
Output is correct |
2 |
Correct |
4171 ms |
657904 KB |
Output is correct |
3 |
Correct |
277 ms |
261788 KB |
Output is correct |