// Source: https://oj.uz/problem/view/COCI17_sirni
//
#include "bits/stdc++.h"
using namespace std;
#define s second
#define f first
#define pb push_back
typedef long long ll;
typedef pair<ll, ll> pii;
typedef vector<pii> vpii;
typedef vector<ll> vi;
#define FOR(i, a, b) for (ll i = (a); i<b; i++)
bool ckmin(ll& a, ll b){ return b < a ? a = b, true : false; }
bool ckmax(ll& a, ll b){ return b > a ? a = b, true : false; }
set<ll> uni;
const ll P = 1e7 + 10;
vector<pair<ll, pii> > ed;
struct DSU {
vector<ll> e;
DSU(ll N) { e = vector<ll>(N, -1); }
// get representive component (uses path compression)
ll get(ll x) { return e[x] < 0 ? x : e[x] = get(e[x]); }
bool same_set(ll a, ll b) { return get(a) == get(b); }
ll size(ll x) { return -e[get(x)]; }
bool unite(ll x, ll y) { // union by size
x = get(x), y = get(y);
if (x == y) return false;
if (e[x] > e[y]) swap(x, y);
e[x] += e[y];
e[y] = x;
return true;
}
};
DSU dsu(P);
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
FOR(i, 1, n + 1) {
ll k;
cin >> k;
uni.insert(k);
}
for (auto cur: uni) {
for (ll e = cur; e < P; e += cur) {
// ll ub = e + cur - 1;
auto it = uni.lower_bound(e);
if (e == cur) it = uni.upper_bound(e);
if (it == uni.end()) continue;
int val = *it;
if (val < e + cur) ed.pb({val % cur, {cur, val}});
// cout << cur << val << endl;
// if (it == uni.begin()) continue;
// ll val = *prev(it, 1);
// while (val >= e) {
// cout << cur << val << endl;
// ed.pb({val - e, {cur, val}});
// ub = (e + val) / 2;
// if (val == e) break;
// it = uni.upper_bound(ub);
// if (it == uni.begin()) break;
// val = *prev(it, 1);
// }
}
}
sort(ed.begin(), ed.end());
ll sz = 0;
for (auto e: ed) {
if (dsu.unite(e.s.s, e.s.f)) sz += e.f;
}
cout << sz << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
78936 KB |
Output is correct |
2 |
Correct |
77 ms |
86804 KB |
Output is correct |
3 |
Correct |
14 ms |
79192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
92 ms |
78932 KB |
Output is correct |
2 |
Correct |
563 ms |
80212 KB |
Output is correct |
3 |
Correct |
12 ms |
79284 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
78940 KB |
Output is correct |
2 |
Correct |
10 ms |
78684 KB |
Output is correct |
3 |
Correct |
12 ms |
78940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
434 ms |
109816 KB |
Output is correct |
2 |
Correct |
1586 ms |
183212 KB |
Output is correct |
3 |
Correct |
701 ms |
132216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
65 ms |
86208 KB |
Output is correct |
2 |
Correct |
644 ms |
177892 KB |
Output is correct |
3 |
Correct |
609 ms |
108468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
995 ms |
182944 KB |
Output is correct |
2 |
Correct |
2153 ms |
281872 KB |
Output is correct |
3 |
Correct |
648 ms |
134572 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
315 ms |
92804 KB |
Output is correct |
2 |
Correct |
1902 ms |
279932 KB |
Output is correct |
3 |
Correct |
637 ms |
133796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
374 ms |
133544 KB |
Output is correct |
2 |
Runtime error |
2120 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
364 ms |
133168 KB |
Output is correct |
2 |
Runtime error |
2226 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
87460 KB |
Output is correct |
2 |
Runtime error |
1898 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |