#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
using ll = long long;
using namespace std;
ll my_pow(ll bs, ll pw)
{
ll i, r;
r = 1;
for(i = 0; i < pw; i++)
r *= bs;
return r;
}
ll lim0(ll count, ll bs)
{
ll MAXD = log(1e20) / log(bs);
ll i, j, c, cc, ec, r, d[MAXD];
bool p;
ec = 0;
c = 0;
p = false;
for(i = 0; i < MAXD; i++)
{
cc = c;
d[i] = 0;
while(true)
{
if(i < MAXD - 1)
{
if(d[i])
cc += ((MAXD - i - 1) * my_pow(bs, MAXD - i - 2));
else
{
if(i == MAXD - 2)
cc++;
else
{
cc += ((MAXD - i - 2) * my_pow(bs, MAXD - i - 2));
for(j = 0; j < MAXD - i - 2; j++)
cc -= my_pow(bs, j);
cc += MAXD - i - 1;
}
if(p)
cc += my_pow(bs, MAXD - i - 1) - 1;
}
}
cc += ec * my_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] * my_pow(bs, MAXD - i - 1);
return r;
}
ll lim(ll count, ll num, ll bs)
{
ll MAXD = log(1e15) / 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) * my_pow(bs, MAXD - i - 2));
if(d[i] == num)
cc += my_pow(bs, MAXD - i - 1) - 1;
}
cc += ((ll) (d[i] + 1 == num)) + ec * my_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] * my_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 | 1816 KiB | ||||
2 | Accepted | 3ms | 2052 KiB | ||||
3 | Accepted | 3ms | 2540 KiB | ||||
subtask2 | 0/25 | ||||||
4 | Wrong answer | 3ms | 2664 KiB | ||||
5 | Wrong answer | 3ms | 2664 KiB | ||||
6 | Wrong answer | 3ms | 2708 KiB | ||||
7 | Wrong answer | 3ms | 2680 KiB | ||||
8 | Accepted | 3ms | 2808 KiB | ||||
9 | Accepted | 3ms | 3048 KiB | ||||
10 | Wrong answer | 3ms | 3104 KiB | ||||
11 | Wrong answer | 2ms | 3108 KiB | ||||
12 | Wrong answer | 3ms | 3108 KiB | ||||
13 | Accepted | 3ms | 3232 KiB | ||||
14 | Accepted | 3ms | 3508 KiB | ||||
15 | Wrong answer | 3ms | 3448 KiB | ||||
16 | Accepted | 3ms | 3572 KiB | ||||
17 | Accepted | 3ms | 3640 KiB | ||||
18 | Accepted | 3ms | 3524 KiB | ||||
19 | Wrong answer | 3ms | 3460 KiB | ||||
subtask3 | 0/10 | ||||||
20 | Wrong answer | 3ms | 3580 KiB | ||||
21 | Wrong answer | 3ms | 3604 KiB | ||||
22 | Wrong answer | 3ms | 3460 KiB | ||||
23 | Accepted | 3ms | 3488 KiB | ||||
24 | Wrong answer | 3ms | 3472 KiB | ||||
subtask4 | 0/15 | ||||||
25 | Wrong answer | 3ms | 3468 KiB | ||||
26 | Wrong answer | 3ms | 3584 KiB | ||||
27 | Wrong answer | 2ms | 3464 KiB | ||||
28 | Accepted | 3ms | 3584 KiB | ||||
29 | Accepted | 3ms | 3456 KiB | ||||
30 | Accepted | 3ms | 3580 KiB | ||||
31 | Accepted | 3ms | 3456 KiB | ||||
32 | Accepted | 3ms | 3460 KiB | ||||
33 | Accepted | 3ms | 3464 KiB | ||||
subtask5 | 0/20 | ||||||
34 | Accepted | 2ms | 3472 KiB | ||||
35 | Accepted | 3ms | 3584 KiB | ||||
36 | Wrong answer | 3ms | 3708 KiB | ||||
37 | Wrong answer | 3ms | 3908 KiB | ||||
38 | Accepted | 3ms | 3920 KiB | ||||
39 | Accepted | 3ms | 3808 KiB | ||||
40 | Accepted | 3ms | 3808 KiB | ||||
41 | Accepted | 3ms | 3808 KiB | ||||
42 | Accepted | 3ms | 3804 KiB | ||||
subtask6 | 0/30 | ||||||
43 | Wrong answer | 3ms | 3956 KiB | ||||
44 | Wrong answer | 3ms | 3804 KiB | ||||
45 | Wrong answer | 3ms | 3932 KiB | ||||
46 | Accepted | 3ms | 3808 KiB | ||||
47 | Accepted | 3ms | 3804 KiB | ||||
48 | Wrong answer | 3ms | 3804 KiB | ||||
49 | Accepted | 3ms | 3812 KiB | ||||
50 | Accepted | 3ms | 3804 KiB | ||||
51 | Accepted | 3ms | 3932 KiB | ||||
52 | Wrong answer | 3ms | 3808 KiB | ||||
53 | Wrong answer | 3ms | 3808 KiB | ||||
54 | Wrong answer | 3ms | 3804 KiB | ||||
55 | Accepted | 3ms | 3808 KiB | ||||
56 | Accepted | 3ms | 3836 KiB | ||||
57 | Accepted | 3ms | 3812 KiB | ||||
58 | Accepted | 3ms | 3808 KiB | ||||
59 | Accepted | 3ms | 3808 KiB | ||||
60 | Wrong answer | 3ms | 3808 KiB |