#include <bits/stdc++.h>
#ifdef DEBUG
#include "debug.hpp"
#endif
using namespace std;
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define traverse(c, it) for(auto it = (c).begin(); it != (c).end(); ++it)
#define rep(i, N) for(int i = 0; i < (N); ++i)
#define rrep(i, N) for(int i = (N) - 1; i >= 0; --i)
#define rep1(i, N) for(int i = 1; i <= (N); ++i)
#define rep2(i, s, e) for(int i = (s); i <= (e); ++i)
#ifdef DEBUG
#define debug(x...) { \
++dbg::depth; \
string dbg_vals = dbg::to_string(x); \
--dbg::depth; \
dbg::fprint(__func__, __LINE__, #x, dbg_vals); \
}
#define light_debug(x) { \
dbg::light = true; \
dbg::dout << __func__ << ":" << __LINE__; \
dbg::dout << " " << #x << " = " << x << endl; \
dbg::light = false; \
}
#else
#define debug(x...) 42
#define light_debug(x) 42
#endif
using ll = long long;
template<typename T>
inline T& ckmin(T& a, T b) { return a = a > b ? b : a; }
template<typename T>
inline T& ckmax(T& a, T b) { return a = a < b ? b : a; }
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
int n; cin >> n;
vector<int> p(n); rep(i, n) cin >> p[i];
sort(all(p)); p.erase(unique(all(p)), p.end());
n = int(p.size());
int m{p.back()};
vector<vector<pair<int, int>>> edges(m + 1);
rep(i, n) {
for(int x{p[i]}; x <= m; x += p[i]) {
int j = int(lower_bound(all(p), x == p[i] ? x + 1 : x) - p.begin());
if(j < n && p[j] < x + p[i])
edges[p[j] - x].push_back({i, j});
}
}
debug(edges);
vector<int> pre(n); iota(all(pre), 0);
auto root = [&](int i) {
int j;
for(j = i; pre[j] != j; j = pre[j]);
for(int t{i}; i != j; i = pre[i], pre[t] = j, t = i);
return j;
};
long long ans{};
auto merge = [&](int i, int j, int c) {
debug(i, j);
i = root(i), j = root(j);
if(i == j) return false;
pre[i] = j;
ans += c;
debug(ans);
return true;
};
int c{};
rep(i, m + 1) {
if(c == n - 1) break;
for(auto [u, v] : edges[i]) {
if(c == n - 1) break;
c += merge(u, v, i);
}
}
cout << ans << '\n';
#ifdef DEBUG
dbg::dout << "\nExecution time: "
<< clock() * 1000 / CLOCKS_PER_SEC
<< "ms" << endl;
#endif
return 0;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
sirni.cpp:65:5: note: in expansion of macro 'debug'
65 | debug(edges);
| ^~~~~
sirni.cpp: In lambda function:
sirni.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
sirni.cpp:79:9: note: in expansion of macro 'debug'
79 | debug(i, j);
| ^~~~~
sirni.cpp:33:29: warning: statement has no effect [-Wunused-value]
33 | #define debug(x...) 42
| ^~
sirni.cpp:84:9: note: in expansion of macro 'debug'
84 | debug(ans);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
124 ms |
235072 KB |
Output is correct |
2 |
Correct |
176 ms |
236600 KB |
Output is correct |
3 |
Correct |
125 ms |
234440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
588 KB |
Output is correct |
2 |
Correct |
656 ms |
235448 KB |
Output is correct |
3 |
Correct |
128 ms |
235144 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
137 ms |
235116 KB |
Output is correct |
2 |
Correct |
123 ms |
234688 KB |
Output is correct |
3 |
Correct |
126 ms |
235248 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
165 ms |
33660 KB |
Output is correct |
2 |
Correct |
477 ms |
61464 KB |
Output is correct |
3 |
Correct |
207 ms |
39480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
25536 KB |
Output is correct |
2 |
Correct |
185 ms |
46508 KB |
Output is correct |
3 |
Correct |
153 ms |
22628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
359 ms |
45856 KB |
Output is correct |
2 |
Correct |
601 ms |
76356 KB |
Output is correct |
3 |
Correct |
200 ms |
38188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
54 ms |
8484 KB |
Output is correct |
2 |
Correct |
534 ms |
70828 KB |
Output is correct |
3 |
Correct |
189 ms |
37480 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
298 ms |
247804 KB |
Output is correct |
2 |
Correct |
2598 ms |
444508 KB |
Output is correct |
3 |
Correct |
332 ms |
250932 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
326 ms |
247576 KB |
Output is correct |
2 |
Correct |
3719 ms |
535264 KB |
Output is correct |
3 |
Correct |
454 ms |
254852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
149 ms |
237384 KB |
Output is correct |
2 |
Correct |
3424 ms |
530944 KB |
Output is correct |
3 |
Correct |
206 ms |
39244 KB |
Output is correct |