#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();
vector<array<int,2>> edges;
if (A.back() <= 1e6) {
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();
}
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});
}
}
}
}
else {
for (int i = 0; i < N; i++) {
for (int j = i+1; j < N; j++) {
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:71: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]
71 | for (int i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
169 ms |
6348 KB |
Output is correct |
2 |
Correct |
139 ms |
4556 KB |
Output is correct |
3 |
Correct |
198 ms |
5836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
600 KB |
Output is correct |
2 |
Correct |
75 ms |
2512 KB |
Output is correct |
3 |
Correct |
213 ms |
4812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
5064 KB |
Output is correct |
2 |
Correct |
153 ms |
5580 KB |
Output is correct |
3 |
Correct |
202 ms |
6348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
372 ms |
21692 KB |
Output is correct |
2 |
Correct |
1485 ms |
38020 KB |
Output is correct |
3 |
Correct |
781 ms |
38236 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
67 ms |
6344 KB |
Output is correct |
2 |
Correct |
866 ms |
39064 KB |
Output is correct |
3 |
Correct |
372 ms |
20004 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
792 ms |
37632 KB |
Output is correct |
2 |
Correct |
2124 ms |
71108 KB |
Output is correct |
3 |
Correct |
670 ms |
22972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
118 ms |
5832 KB |
Output is correct |
2 |
Correct |
2226 ms |
71244 KB |
Output is correct |
3 |
Correct |
720 ms |
21176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
759 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
756 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
719 ms |
786432 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |