#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int base;
map<pair<ll, 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(pair<ll, int>(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[pair<ll, int>(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 = 55; 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 | 1824 KiB | ||||
2 | Accepted | 3ms | 2208 KiB | ||||
3 | Accepted | 4ms | 2572 KiB | ||||
subtask2 | 25/25 | ||||||
4 | Accepted | 3ms | 2460 KiB | ||||
5 | Accepted | 3ms | 2672 KiB | ||||
6 | Accepted | 3ms | 2880 KiB | ||||
7 | Accepted | 3ms | 3272 KiB | ||||
8 | Accepted | 3ms | 3224 KiB | ||||
9 | Accepted | 3ms | 3404 KiB | ||||
10 | Accepted | 3ms | 3316 KiB | ||||
11 | Accepted | 4ms | 3512 KiB | ||||
12 | Accepted | 3ms | 3384 KiB | ||||
13 | Accepted | 3ms | 3716 KiB | ||||
14 | Accepted | 3ms | 3708 KiB | ||||
15 | Accepted | 3ms | 3684 KiB | ||||
16 | Accepted | 3ms | 3604 KiB | ||||
17 | Accepted | 3ms | 3988 KiB | ||||
18 | Accepted | 3ms | 3932 KiB | ||||
19 | Accepted | 3ms | 4264 KiB | ||||
subtask3 | 10/10 | ||||||
20 | Accepted | 4ms | 4320 KiB | ||||
21 | Accepted | 4ms | 4640 KiB | ||||
22 | Accepted | 4ms | 4812 KiB | ||||
23 | Accepted | 3ms | 4468 KiB | ||||
24 | Accepted | 4ms | 4772 KiB | ||||
subtask4 | 15/15 | ||||||
25 | Accepted | 4ms | 4780 KiB | ||||
26 | Accepted | 4ms | 4716 KiB | ||||
27 | Accepted | 4ms | 4664 KiB | ||||
28 | Accepted | 4ms | 4788 KiB | ||||
29 | Accepted | 4ms | 4788 KiB | ||||
30 | Accepted | 3ms | 4528 KiB | ||||
31 | Accepted | 4ms | 4888 KiB | ||||
32 | Accepted | 4ms | 4912 KiB | ||||
33 | Accepted | 4ms | 4812 KiB | ||||
subtask5 | 20/20 | ||||||
34 | Accepted | 3ms | 4608 KiB | ||||
35 | Accepted | 3ms | 4872 KiB | ||||
36 | Accepted | 4ms | 5032 KiB | ||||
37 | Accepted | 4ms | 5084 KiB | ||||
38 | Accepted | 4ms | 5092 KiB | ||||
39 | Accepted | 4ms | 5208 KiB | ||||
40 | Accepted | 4ms | 5040 KiB | ||||
41 | Accepted | 4ms | 5020 KiB | ||||
42 | Accepted | 4ms | 5116 KiB | ||||
subtask6 | 30/30 | ||||||
43 | Accepted | 4ms | 5376 KiB | ||||
44 | Accepted | 4ms | 5560 KiB | ||||
45 | Accepted | 4ms | 5296 KiB | ||||
46 | Accepted | 4ms | 5348 KiB | ||||
47 | Accepted | 4ms | 5348 KiB | ||||
48 | Accepted | 4ms | 5252 KiB | ||||
49 | Accepted | 3ms | 5072 KiB | ||||
50 | Accepted | 4ms | 5232 KiB | ||||
51 | Accepted | 4ms | 5240 KiB | ||||
52 | Accepted | 2ms | 4940 KiB | ||||
53 | Accepted | 4ms | 5244 KiB | ||||
54 | Accepted | 4ms | 5208 KiB | ||||
55 | Accepted | 4ms | 5140 KiB | ||||
56 | Accepted | 4ms | 5244 KiB | ||||
57 | Accepted | 4ms | 5288 KiB | ||||
58 | Accepted | 4ms | 5552 KiB | ||||
59 | Accepted | 4ms | 5240 KiB | ||||
60 | Accepted | 4ms | 5344 KiB |