#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++)
const int MaxN = 1e5;
const int MaxA = 1e6;
int N, A[MaxN], Freq[MaxA + 1];
struct DSU {
int e[MaxA+1];
void initSz(int n) {
memset(e, -1, n * sizeof(int));
e[MaxA] = n;
}
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;
e[MaxN]--;
return true;
}
};
DSU dsu;
void sub1() {
sort(A, A + N);
vector<array<int,3>> edges;
rep(i, 0, N-1) {
rep(j, i+1, N-1) {
edges.push_back({A[j] % A[i], i, j});
}
}
sort(all(edges));
dsu.initSz(N);
long long res = 0;
for (int i = 0; i < edges.size(); i++) {
int w = edges[i][0];
int u = edges[i][1];
int v = edges[i][2];
if (dsu.unite(u, v)) res += w;
}
cout << res;
}
void sub2() {
sort(A, A + N);
N = unique(A, A + N) - A;
rep(i, 0, N-1) Freq[A[i]]++;
vector<array<int,3>> edges;
for (int i = 0; i < N; i++) {
int a = A[i];
for (int j = a; j <= MaxA; j += a) {
int b = upper_bound(A, A + N, j) - A;
if (b < N) {
b = A[b];
edges.push_back({b % a, a, b});
}
}
}
sort(all(edges));
dsu.initSz(MaxA + 1);
long long res = 0;
for (int i = 0; i < edges.size(); i++) {
int w = edges[i][0];
int u = edges[i][1];
int v = edges[i][2];
if (dsu.unite(u, v)) res += w;
}
cout << res;
}
signed main() {
cin >> N;
for (int i = 0; i < N; i++) {
cin >> A[i];
}
if (N <= 1e3) {
sub1();
}
else {
sub2();
}
return 0;
}
Compilation message
sirni.cpp: In function 'void sub1()':
sirni.cpp:45:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
45 | for (int i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
sirni.cpp: In function 'void sub2()':
sirni.cpp:77:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::array<int, 3> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | for (int i = 0; i < edges.size(); i++) {
| ~~^~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
9656 KB |
Output is correct |
2 |
Correct |
59 ms |
10736 KB |
Output is correct |
3 |
Correct |
51 ms |
10396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
58 ms |
10172 KB |
Output is correct |
2 |
Correct |
67 ms |
10532 KB |
Output is correct |
3 |
Correct |
50 ms |
10424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
57 ms |
10536 KB |
Output is correct |
2 |
Correct |
39 ms |
10420 KB |
Output is correct |
3 |
Correct |
50 ms |
10416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
265 ms |
32080 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
39 ms |
11200 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
569 ms |
57460 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
107 ms |
12980 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
36 ms |
13404 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
35 ms |
13396 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
12 ms |
12376 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |