#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
using ll = long long;
using namespace std;
ll lim(ll count, ll num, ll bs)
{
ll MAXD = log(1e13) / log(bs);
ll i, c, cc, ec, r, d[MAXD];
c = ec = 0;
for(i = 0; i < MAXD; i++)
{
cc = c;
d[i] = 0;
while(true)
{
if(i < MAXD - 1)
{
cc += ((MAXD - i - 1) * pow(bs, MAXD - i - 2));
if(d[i] == num)
cc += pow(bs, MAXD - i - 1) - 1;
}
cc += ((ll) (d[i] + 1 == num)) + ec * pow(bs, MAXD - i - 1);
if(d[i] < (bs - 1) && cc <= count)
{
c = cc;
d[i]++;
}
else
break;
}
ec += ((ll) (d[i] == num));
}
r = 0;
for(i = 0; i < MAXD; i++)
r += d[i] * pow(bs, MAXD - i - 1);
return r;
}
ll lim0(ll count, ll bs)
{
ll MAXD = log(1e13) / log(bs);
ll i, j, c, cc, ec, r, d[MAXD];
bool p;
c = ec = 0;
p = false;
for(i = 0; i < MAXD; i++)
{
cc = c;
d[i] = 0;
while(true)
{
if(i < MAXD - 1)
{
if(!d[i] && !p)
{
if(i == MAXD - 2)
cc++;
else
{
cc += ((MAXD - i - 2) * pow(bs, MAXD - i - 2));
for(j = 0; j < MAXD - i - 2; j++)
cc -= pow(bs, j);
cc += MAXD - i - 1;
}
}
else
cc += ((MAXD - i - 1) * pow(bs, MAXD - i - 2));
if(!d[i] && p)
cc += pow(bs, MAXD - i - 1) - 1;
}
cc += ec * pow(bs, MAXD - i - 1);
if(d[i] < (bs - 1) && cc <= count)
{
c = cc;
d[i]++;
}
else
break;
}
if(d[i])
p = true;
else if(p)
ec++;
}
r = 0;
for(i = 0; i < MAXD; i++)
r += d[i] * pow(bs, MAXD - i - 1);
return r;
}
int main()
{
ll i, ans, K;
cin >> K;
vector<ll> d(K);
for(i = 0; i < K; i++)
cin >> d[i];
ans = lim0(d[0], K);
for(i = 1; i < K; i++)
ans = min(lim(d[i], i, K), ans);
cout << ans << endl;
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1808 KiB | ||||
2 | Accepted | 3ms | 2064 KiB | ||||
3 | Accepted | 3ms | 2276 KiB | ||||
subtask2 | 25/25 | ||||||
4 | Accepted | 3ms | 2488 KiB | ||||
5 | Accepted | 3ms | 2724 KiB | ||||
6 | Accepted | 2ms | 2804 KiB | ||||
7 | Accepted | 3ms | 2808 KiB | ||||
8 | Accepted | 3ms | 2768 KiB | ||||
9 | Accepted | 3ms | 2968 KiB | ||||
10 | Accepted | 3ms | 3180 KiB | ||||
11 | Accepted | 3ms | 3296 KiB | ||||
12 | Accepted | 2ms | 3376 KiB | ||||
13 | Accepted | 2ms | 3532 KiB | ||||
14 | Accepted | 3ms | 3628 KiB | ||||
15 | Accepted | 3ms | 3628 KiB | ||||
16 | Accepted | 2ms | 3708 KiB | ||||
17 | Accepted | 3ms | 3840 KiB | ||||
18 | Accepted | 3ms | 3932 KiB | ||||
19 | Accepted | 2ms | 4020 KiB | ||||
subtask3 | 10/10 | ||||||
20 | Accepted | 3ms | 4148 KiB | ||||
21 | Accepted | 3ms | 4388 KiB | ||||
22 | Accepted | 2ms | 4476 KiB | ||||
23 | Accepted | 2ms | 4368 KiB | ||||
24 | Accepted | 2ms | 4368 KiB | ||||
subtask4 | 15/15 | ||||||
25 | Accepted | 3ms | 4368 KiB | ||||
26 | Accepted | 3ms | 4608 KiB | ||||
27 | Accepted | 2ms | 4688 KiB | ||||
28 | Accepted | 2ms | 4700 KiB | ||||
29 | Accepted | 2ms | 4788 KiB | ||||
30 | Accepted | 2ms | 4812 KiB | ||||
31 | Accepted | 2ms | 4784 KiB | ||||
32 | Accepted | 3ms | 4888 KiB | ||||
33 | Accepted | 2ms | 4768 KiB | ||||
subtask5 | 20/20 | ||||||
34 | Accepted | 2ms | 4884 KiB | ||||
35 | Accepted | 2ms | 4912 KiB | ||||
36 | Accepted | 2ms | 4772 KiB | ||||
37 | Accepted | 2ms | 4880 KiB | ||||
38 | Accepted | 3ms | 4892 KiB | ||||
39 | Accepted | 3ms | 4968 KiB | ||||
40 | Accepted | 3ms | 4964 KiB | ||||
41 | Accepted | 3ms | 4968 KiB | ||||
42 | Accepted | 2ms | 4968 KiB | ||||
subtask6 | 30/30 | ||||||
43 | Accepted | 3ms | 4964 KiB | ||||
44 | Accepted | 2ms | 4968 KiB | ||||
45 | Accepted | 3ms | 5124 KiB | ||||
46 | Accepted | 3ms | 5128 KiB | ||||
47 | Accepted | 3ms | 5292 KiB | ||||
48 | Accepted | 2ms | 5176 KiB | ||||
49 | Accepted | 3ms | 5300 KiB | ||||
50 | Accepted | 3ms | 5184 KiB | ||||
51 | Accepted | 3ms | 5208 KiB | ||||
52 | Accepted | 3ms | 5184 KiB | ||||
53 | Accepted | 2ms | 5180 KiB | ||||
54 | Accepted | 2ms | 5180 KiB | ||||
55 | Accepted | 3ms | 5176 KiB | ||||
56 | Accepted | 3ms | 5176 KiB | ||||
57 | Accepted | 3ms | 5180 KiB | ||||
58 | Accepted | 2ms | 5184 KiB | ||||
59 | Accepted | 3ms | 5180 KiB | ||||
60 | Accepted | 3ms | 5180 KiB |