#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int base;
map<pair<int, int>, ll> mem;
// I have no idea how this works, just started messing around with formulas on
// OEIS and got this working
// calculates the number of `d` digits used from `1..n`
ll pog(ll n, int d) {
if (n <= 0)
return 0;
if (mem.count({n, d})) {
return mem.at({n, d});
}
ll r = n % base;
ll q = n / base;
ll res = q + (r + 1) * pog(q, d) + (base - r - 1) * pog(q - 1, d);
if (d != 0 && r >= d) {
res++;
}
return mem[make_pair(n, d)] = res;
}
int main() {
cin >> base;
vector<ll> budget(base);
for (auto &b : budget)
cin >> b;
auto is_enough = [&](ll n) {
for (int i = 0; i < base; i++) {
ll a = pog(n, i);
if (a > budget[i])
return false;
}
return true;
};
ll curr = 0;
for (int i = 28; i >= 0; i--) {
ll next = curr + (1ll << i);
if (is_enough(next)) {
curr = next;
}
}
cout << curr;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1812 KiB | ||||
2 | Accepted | 3ms | 2228 KiB | ||||
3 | Wrong answer | 4ms | 2512 KiB | ||||
subtask2 | 25/25 | ||||||
4 | Accepted | 3ms | 2344 KiB | ||||
5 | Accepted | 3ms | 2456 KiB | ||||
6 | Accepted | 3ms | 2668 KiB | ||||
7 | Accepted | 3ms | 2660 KiB | ||||
8 | Accepted | 3ms | 2912 KiB | ||||
9 | Accepted | 3ms | 2888 KiB | ||||
10 | Accepted | 3ms | 2980 KiB | ||||
11 | Accepted | 3ms | 3084 KiB | ||||
12 | Accepted | 3ms | 3196 KiB | ||||
13 | Accepted | 3ms | 3460 KiB | ||||
14 | Accepted | 3ms | 3352 KiB | ||||
15 | Accepted | 3ms | 3616 KiB | ||||
16 | Accepted | 3ms | 3484 KiB | ||||
17 | Accepted | 3ms | 3500 KiB | ||||
18 | Accepted | 3ms | 3496 KiB | ||||
19 | Accepted | 3ms | 3492 KiB | ||||
subtask3 | 0/10 | ||||||
20 | Wrong answer | 4ms | 3852 KiB | ||||
21 | Wrong answer | 3ms | 4060 KiB | ||||
22 | Wrong answer | 3ms | 4276 KiB | ||||
23 | Accepted | 3ms | 4092 KiB | ||||
24 | Wrong answer | 4ms | 4360 KiB | ||||
subtask4 | 0/15 | ||||||
25 | Wrong answer | 3ms | 4364 KiB | ||||
26 | Wrong answer | 4ms | 4208 KiB | ||||
27 | Wrong answer | 4ms | 4488 KiB | ||||
28 | Wrong answer | 3ms | 4556 KiB | ||||
29 | Wrong answer | 4ms | 4564 KiB | ||||
30 | Accepted | 3ms | 4324 KiB | ||||
31 | Wrong answer | 4ms | 4716 KiB | ||||
32 | Wrong answer | 4ms | 4724 KiB | ||||
33 | Wrong answer | 4ms | 4824 KiB | ||||
subtask5 | 0/20 | ||||||
34 | Accepted | 3ms | 4536 KiB | ||||
35 | Accepted | 3ms | 4528 KiB | ||||
36 | Wrong answer | 4ms | 4804 KiB | ||||
37 | Wrong answer | 4ms | 4812 KiB | ||||
38 | Wrong answer | 4ms | 4808 KiB | ||||
39 | Wrong answer | 4ms | 4804 KiB | ||||
40 | Wrong answer | 4ms | 4812 KiB | ||||
41 | Wrong answer | 4ms | 4812 KiB | ||||
42 | Wrong answer | 4ms | 4936 KiB | ||||
subtask6 | 0/30 | ||||||
43 | Wrong answer | 3ms | 4996 KiB | ||||
44 | Wrong answer | 3ms | 4848 KiB | ||||
45 | Wrong answer | 3ms | 4996 KiB | ||||
46 | Wrong answer | 3ms | 4984 KiB | ||||
47 | Wrong answer | 3ms | 5116 KiB | ||||
48 | Wrong answer | 3ms | 5204 KiB | ||||
49 | Wrong answer | 4ms | 5404 KiB | ||||
50 | Wrong answer | 4ms | 5264 KiB | ||||
51 | Wrong answer | 4ms | 5252 KiB | ||||
52 | Accepted | 3ms | 4964 KiB | ||||
53 | Wrong answer | 4ms | 5076 KiB | ||||
54 | Wrong answer | 4ms | 5224 KiB | ||||
55 | Wrong answer | 3ms | 5212 KiB | ||||
56 | Wrong answer | 3ms | 5352 KiB | ||||
57 | Wrong answer | 3ms | 5360 KiB | ||||
58 | Wrong answer | 4ms | 5396 KiB | ||||
59 | Wrong answer | 4ms | 5244 KiB | ||||
60 | Wrong answer | 4ms | 5352 KiB |