#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 = [&](int 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 | 1816 KiB | ||||
2 | Accepted | 3ms | 2172 KiB | ||||
3 | Wrong answer | 4ms | 2476 KiB | ||||
subtask2 | 25/25 | ||||||
4 | Accepted | 3ms | 2448 KiB | ||||
5 | Accepted | 3ms | 2664 KiB | ||||
6 | Accepted | 3ms | 2880 KiB | ||||
7 | Accepted | 3ms | 3108 KiB | ||||
8 | Accepted | 3ms | 3200 KiB | ||||
9 | Accepted | 3ms | 3456 KiB | ||||
10 | Accepted | 3ms | 3500 KiB | ||||
11 | Accepted | 3ms | 3736 KiB | ||||
12 | Accepted | 3ms | 3680 KiB | ||||
13 | Accepted | 3ms | 4076 KiB | ||||
14 | Accepted | 3ms | 4060 KiB | ||||
15 | Accepted | 3ms | 4276 KiB | ||||
16 | Accepted | 3ms | 4332 KiB | ||||
17 | Accepted | 3ms | 4348 KiB | ||||
18 | Accepted | 3ms | 4408 KiB | ||||
19 | Accepted | 3ms | 4500 KiB | ||||
subtask3 | 0/10 | ||||||
20 | Wrong answer | 3ms | 4736 KiB | ||||
21 | Wrong answer | 3ms | 4736 KiB | ||||
22 | Wrong answer | 3ms | 4740 KiB | ||||
23 | Accepted | 3ms | 4380 KiB | ||||
24 | Wrong answer | 3ms | 4644 KiB | ||||
subtask4 | 0/15 | ||||||
25 | Wrong answer | 4ms | 4696 KiB | ||||
26 | Wrong answer | 3ms | 4648 KiB | ||||
27 | Wrong answer | 4ms | 4928 KiB | ||||
28 | Wrong answer | 3ms | 4912 KiB | ||||
29 | Wrong answer | 3ms | 5084 KiB | ||||
30 | Accepted | 3ms | 4844 KiB | ||||
31 | Wrong answer | 4ms | 5124 KiB | ||||
32 | Wrong answer | 4ms | 5176 KiB | ||||
33 | Wrong answer | 4ms | 5112 KiB | ||||
subtask5 | 0/20 | ||||||
34 | Accepted | 3ms | 4916 KiB | ||||
35 | Accepted | 3ms | 4920 KiB | ||||
36 | Wrong answer | 4ms | 5260 KiB | ||||
37 | Wrong answer | 4ms | 5112 KiB | ||||
38 | Wrong answer | 4ms | 5112 KiB | ||||
39 | Wrong answer | 4ms | 5332 KiB | ||||
40 | Wrong answer | 4ms | 5200 KiB | ||||
41 | Wrong answer | 4ms | 5108 KiB | ||||
42 | Wrong answer | 4ms | 5108 KiB | ||||
subtask6 | 0/30 | ||||||
43 | Wrong answer | 3ms | 5092 KiB | ||||
44 | Wrong answer | 3ms | 5036 KiB | ||||
45 | Wrong answer | 3ms | 5092 KiB | ||||
46 | Wrong answer | 3ms | 5188 KiB | ||||
47 | Wrong answer | 4ms | 5216 KiB | ||||
48 | Wrong answer | 4ms | 5332 KiB | ||||
49 | Wrong answer | 4ms | 5348 KiB | ||||
50 | Wrong answer | 4ms | 5176 KiB | ||||
51 | Wrong answer | 4ms | 5184 KiB | ||||
52 | Accepted | 3ms | 4904 KiB | ||||
53 | Wrong answer | 3ms | 5008 KiB | ||||
54 | Wrong answer | 4ms | 5160 KiB | ||||
55 | Wrong answer | 3ms | 5144 KiB | ||||
56 | Wrong answer | 4ms | 5152 KiB | ||||
57 | Wrong answer | 4ms | 5156 KiB | ||||
58 | Wrong answer | 4ms | 5328 KiB | ||||
59 | Wrong answer | 4ms | 5264 KiB | ||||
60 | Wrong answer | 4ms | 5328 KiB |