#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 = 20; 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 | 1684 KiB | ||||
2 | Accepted | 3ms | 1880 KiB | ||||
3 | Wrong answer | 3ms | 2512 KiB | ||||
subtask2 | 25/25 | ||||||
4 | Accepted | 3ms | 2428 KiB | ||||
5 | Accepted | 3ms | 2528 KiB | ||||
6 | Accepted | 3ms | 2740 KiB | ||||
7 | Accepted | 3ms | 2956 KiB | ||||
8 | Accepted | 3ms | 3048 KiB | ||||
9 | Accepted | 3ms | 3316 KiB | ||||
10 | Accepted | 3ms | 3508 KiB | ||||
11 | Accepted | 3ms | 3676 KiB | ||||
12 | Accepted | 3ms | 3856 KiB | ||||
13 | Accepted | 3ms | 3732 KiB | ||||
14 | Accepted | 3ms | 3724 KiB | ||||
15 | Accepted | 3ms | 3856 KiB | ||||
16 | Accepted | 3ms | 3860 KiB | ||||
17 | Accepted | 3ms | 4000 KiB | ||||
18 | Accepted | 3ms | 4100 KiB | ||||
19 | Accepted | 3ms | 4180 KiB | ||||
subtask3 | 0/10 | ||||||
20 | Wrong answer | 3ms | 4224 KiB | ||||
21 | Wrong answer | 3ms | 4224 KiB | ||||
22 | Wrong answer | 3ms | 4224 KiB | ||||
23 | Accepted | 3ms | 4208 KiB | ||||
24 | Wrong answer | 3ms | 4432 KiB | ||||
subtask4 | 0/15 | ||||||
25 | Wrong answer | 3ms | 4604 KiB | ||||
26 | Wrong answer | 3ms | 4496 KiB | ||||
27 | Wrong answer | 3ms | 4656 KiB | ||||
28 | Wrong answer | 3ms | 4592 KiB | ||||
29 | Wrong answer | 3ms | 4596 KiB | ||||
30 | Accepted | 3ms | 4716 KiB | ||||
31 | Wrong answer | 3ms | 4976 KiB | ||||
32 | Wrong answer | 3ms | 5068 KiB | ||||
33 | Wrong answer | 3ms | 5188 KiB | ||||
subtask5 | 0/20 | ||||||
34 | Accepted | 3ms | 4940 KiB | ||||
35 | Accepted | 3ms | 4984 KiB | ||||
36 | Wrong answer | 3ms | 5144 KiB | ||||
37 | Wrong answer | 3ms | 5068 KiB | ||||
38 | Wrong answer | 3ms | 5164 KiB | ||||
39 | Wrong answer | 3ms | 5168 KiB | ||||
40 | Wrong answer | 3ms | 5172 KiB | ||||
41 | Wrong answer | 3ms | 5168 KiB | ||||
42 | Wrong answer | 3ms | 5324 KiB | ||||
subtask6 | 0/30 | ||||||
43 | Wrong answer | 3ms | 5212 KiB | ||||
44 | Wrong answer | 3ms | 5192 KiB | ||||
45 | Wrong answer | 3ms | 5424 KiB | ||||
46 | Wrong answer | 3ms | 5264 KiB | ||||
47 | Wrong answer | 3ms | 5272 KiB | ||||
48 | Wrong answer | 3ms | 5408 KiB | ||||
49 | Wrong answer | 3ms | 5324 KiB | ||||
50 | Wrong answer | 3ms | 5164 KiB | ||||
51 | Wrong answer | 3ms | 5172 KiB | ||||
52 | Accepted | 3ms | 5132 KiB | ||||
53 | Wrong answer | 3ms | 5328 KiB | ||||
54 | Wrong answer | 3ms | 5300 KiB | ||||
55 | Wrong answer | 3ms | 5160 KiB | ||||
56 | Wrong answer | 3ms | 5164 KiB | ||||
57 | Wrong answer | 3ms | 5176 KiB | ||||
58 | Wrong answer | 3ms | 5188 KiB | ||||
59 | Wrong answer | 3ms | 5180 KiB | ||||
60 | Wrong answer | 3ms | 5316 KiB |