#include <bits/stdc++.h>
using namespace std;
#define all(v) v.begin(),v.end()
using i64 = long long;
#define rep(i,l,r) for (int i = (l); i <= (r); i++)
struct DSU {
vector<int> e;
void initSz(int n) {
e = vector<int>(n, -1);
}
int fr(int x) { return e[x] < 0 ? x : e[x] = fr(e[x]); }
bool unite(int x, int y) {
x = fr(x), y = fr(y);
if (x==y) return false;
if (e[x] > e[y]) swap(x,y);
e[x] += e[y];
e[y] = x;
return true;
}
};
signed main() {
int N; cin >> N;
vector<int> A(N);
for (int& i : A) cin >> i;
sort(all(A));
A.erase(unique(all(A)), A.end());
N = A.size();
int Largest = A.back();
vector<int> lb(Largest+1);
for (int i = 0; i <= Largest; i++) {
lb[i] = lower_bound(A.begin() + lb[i-1], A.end(), i) - A.begin();
}
vector<array<int,2>> edges;
for (int i = 0; i < N-1; i++) {
edges.push_back({i, i+1});
for (int at = 2 * A[i]; at <= Largest; at += A[i]) {
int j = lb[at];
if (j < N) {
edges.push_back({i, j});
}
}
}
sort(all(edges), [&](const array<int,2>& a, const array<int,2>& b) {
auto cost = [&](int i, int j) {
return min(A[i] % A[j], A[j] % A[i]);
};
return cost(a[0], a[1]) < cost(b[0], b[1]);
});
DSU dsu;
dsu.initSz(N);
long long res = 0;
for (int i = 0; i < edges.size(); i++) {
int u = edges[i][0];
int v = edges[i][1];
int w = A[v] % A[u];
res += w * dsu.unite(u, v);
}
cout << res;
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:61:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 2> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
61 | for (int i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
79 ms |
39512 KB |
Output is correct |
2 |
Correct |
1139 ms |
73020 KB |
Output is correct |
3 |
Correct |
81 ms |
39672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
344 KB |
Output is correct |
2 |
Execution timed out |
5042 ms |
565124 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
80 ms |
39552 KB |
Output is correct |
2 |
Correct |
69 ms |
39508 KB |
Output is correct |
3 |
Correct |
78 ms |
39812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
370 ms |
21436 KB |
Output is correct |
2 |
Correct |
1491 ms |
38276 KB |
Output is correct |
3 |
Correct |
779 ms |
38072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
66 ms |
6548 KB |
Output is correct |
2 |
Correct |
867 ms |
38264 KB |
Output is correct |
3 |
Correct |
371 ms |
19128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
792 ms |
38064 KB |
Output is correct |
2 |
Correct |
2125 ms |
72104 KB |
Output is correct |
3 |
Correct |
664 ms |
22460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
117 ms |
6604 KB |
Output is correct |
2 |
Correct |
2220 ms |
71912 KB |
Output is correct |
3 |
Correct |
718 ms |
22952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
598 ms |
57716 KB |
Output is correct |
2 |
Execution timed out |
5041 ms |
565856 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
783 ms |
58044 KB |
Output is correct |
2 |
Execution timed out |
5066 ms |
565316 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
41672 KB |
Output is correct |
2 |
Execution timed out |
5096 ms |
566516 KB |
Time limit exceeded |
3 |
Halted |
0 ms |
0 KB |
- |