// 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;
vpii ed[P];
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);
ll nxt[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);
}
ll largest = *prev(uni.end(), 1);
ll cur = largest;
FOR(i, largest + 1, P) nxt[i] = -1;
for (ll i = largest; i >= 0; i--) {
cur = *uni.lower_bound(i);
nxt[i] = cur;
}
for (auto cur: uni) {
for (ll e = cur; e < P; e += cur) {
// ll ub = e + cur - 1;
ll val = nxt[e];
if (e == cur) val = nxt[e+1];
if (val == -1) break;
if (val < e + cur) ed[val % cur].pb({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);
// }
}
}
ll sz = 0;
FOR(c, 0, P) {
for (auto e: ed[c])
if (dsu.unite(e.f, e.s)) sz += c;
}
cout << sz << endl;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
206 ms |
391920 KB |
Output is correct |
2 |
Correct |
204 ms |
397260 KB |
Output is correct |
3 |
Correct |
171 ms |
392076 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
87 ms |
392016 KB |
Output is correct |
2 |
Correct |
409 ms |
393300 KB |
Output is correct |
3 |
Correct |
170 ms |
392020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
170 ms |
391888 KB |
Output is correct |
2 |
Correct |
161 ms |
392116 KB |
Output is correct |
3 |
Correct |
166 ms |
392016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
180 ms |
415236 KB |
Output is correct |
2 |
Correct |
265 ms |
475580 KB |
Output is correct |
3 |
Correct |
200 ms |
425496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
108 ms |
395340 KB |
Output is correct |
2 |
Correct |
180 ms |
434380 KB |
Output is correct |
3 |
Correct |
158 ms |
414316 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
212 ms |
445156 KB |
Output is correct |
2 |
Correct |
274 ms |
502432 KB |
Output is correct |
3 |
Correct |
182 ms |
423620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
110 ms |
399728 KB |
Output is correct |
2 |
Correct |
265 ms |
490348 KB |
Output is correct |
3 |
Correct |
175 ms |
420960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
333 ms |
418540 KB |
Output is correct |
2 |
Runtime error |
843 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
339 ms |
419724 KB |
Output is correct |
2 |
Runtime error |
920 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
232 ms |
397204 KB |
Output is correct |
2 |
Runtime error |
1012 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |