// 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; }
const ll P = 1e7 + 10;
vpii ed[P];
bool vals[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];
set<int> uni;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
ll n;
cin >> n;
FOR(i, 1, n + 1) {
ll k;
cin >> k;
vals[k]=true;
uni.insert(k);
}
ll largest = -1;
ll cur = largest;
for (ll i = P-1; i >= 0; i--) {
if (vals[i]) largest=i;
// if (largest != -1) cout << largest << endl;
nxt[i] = largest;
}
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;
}
Compilation message
sirni.cpp: In function 'int main()':
sirni.cpp:73:5: warning: unused variable 'cur' [-Wunused-variable]
73 | ll cur = largest;
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
205 ms |
396188 KB |
Output is correct |
2 |
Correct |
184 ms |
399872 KB |
Output is correct |
3 |
Correct |
147 ms |
396628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
141 ms |
393556 KB |
Output is correct |
2 |
Correct |
448 ms |
395524 KB |
Output is correct |
3 |
Correct |
141 ms |
396628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
154 ms |
396376 KB |
Output is correct |
2 |
Correct |
143 ms |
395448 KB |
Output is correct |
3 |
Correct |
144 ms |
396376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
418484 KB |
Output is correct |
2 |
Correct |
296 ms |
472896 KB |
Output is correct |
3 |
Correct |
211 ms |
428992 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
142 ms |
399184 KB |
Output is correct |
2 |
Correct |
235 ms |
438436 KB |
Output is correct |
3 |
Correct |
193 ms |
418556 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
241 ms |
443792 KB |
Output is correct |
2 |
Correct |
320 ms |
502268 KB |
Output is correct |
3 |
Correct |
208 ms |
426260 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
157 ms |
403740 KB |
Output is correct |
2 |
Correct |
289 ms |
490912 KB |
Output is correct |
3 |
Correct |
202 ms |
425352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
239 ms |
428984 KB |
Output is correct |
2 |
Runtime error |
901 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
245 ms |
430284 KB |
Output is correct |
2 |
Runtime error |
1039 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
156 ms |
407092 KB |
Output is correct |
2 |
Runtime error |
1006 ms |
786432 KB |
Execution killed with signal 9 |
3 |
Halted |
0 ms |
0 KB |
- |