#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(1e15) / 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 | 2056 KiB | ||||
3 | Accepted | 3ms | 2212 KiB | ||||
subtask2 | 0/25 | ||||||
4 | Accepted | 3ms | 2428 KiB | ||||
5 | Wrong answer | 2ms | 2512 KiB | ||||
6 | Accepted | 2ms | 2512 KiB | ||||
7 | Accepted | 2ms | 2640 KiB | ||||
8 | Accepted | 3ms | 2648 KiB | ||||
9 | Accepted | 3ms | 2900 KiB | ||||
10 | Wrong answer | 3ms | 2808 KiB | ||||
11 | Accepted | 3ms | 2936 KiB | ||||
12 | Accepted | 3ms | 3148 KiB | ||||
13 | Accepted | 3ms | 3260 KiB | ||||
14 | Accepted | 3ms | 3264 KiB | ||||
15 | Wrong answer | 2ms | 3380 KiB | ||||
16 | Accepted | 3ms | 3620 KiB | ||||
17 | Accepted | 2ms | 3508 KiB | ||||
18 | Accepted | 3ms | 3476 KiB | ||||
19 | Wrong answer | 3ms | 3724 KiB | ||||
subtask3 | 10/10 | ||||||
20 | Accepted | 3ms | 3572 KiB | ||||
21 | Accepted | 3ms | 3576 KiB | ||||
22 | Accepted | 3ms | 3700 KiB | ||||
23 | Accepted | 3ms | 3916 KiB | ||||
24 | Accepted | 3ms | 3992 KiB | ||||
subtask4 | 15/15 | ||||||
25 | Accepted | 3ms | 4124 KiB | ||||
26 | Accepted | 3ms | 4336 KiB | ||||
27 | Accepted | 2ms | 4420 KiB | ||||
28 | Accepted | 3ms | 4428 KiB | ||||
29 | Accepted | 3ms | 4560 KiB | ||||
30 | Accepted | 2ms | 4628 KiB | ||||
31 | Accepted | 2ms | 4632 KiB | ||||
32 | Accepted | 2ms | 4628 KiB | ||||
33 | Accepted | 3ms | 4752 KiB | ||||
subtask5 | 0/20 | ||||||
34 | Accepted | 3ms | 4752 KiB | ||||
35 | Accepted | 2ms | 4632 KiB | ||||
36 | Wrong answer | 3ms | 4656 KiB | ||||
37 | Wrong answer | 3ms | 4636 KiB | ||||
38 | Accepted | 3ms | 4660 KiB | ||||
39 | Accepted | 2ms | 4732 KiB | ||||
40 | Accepted | 2ms | 4740 KiB | ||||
41 | Accepted | 3ms | 4756 KiB | ||||
42 | Accepted | 3ms | 4856 KiB | ||||
subtask6 | 0/30 | ||||||
43 | Wrong answer | 3ms | 4948 KiB | ||||
44 | Wrong answer | 3ms | 5052 KiB | ||||
45 | Wrong answer | 3ms | 4940 KiB | ||||
46 | Accepted | 3ms | 5056 KiB | ||||
47 | Accepted | 3ms | 4940 KiB | ||||
48 | Wrong answer | 3ms | 4944 KiB | ||||
49 | Accepted | 2ms | 4940 KiB | ||||
50 | Accepted | 3ms | 4944 KiB | ||||
51 | Accepted | 2ms | 4944 KiB | ||||
52 | Accepted | 2ms | 4944 KiB | ||||
53 | Accepted | 3ms | 4940 KiB | ||||
54 | Accepted | 2ms | 4944 KiB | ||||
55 | Accepted | 3ms | 4968 KiB | ||||
56 | Accepted | 2ms | 5052 KiB | ||||
57 | Accepted | 2ms | 4948 KiB | ||||
58 | Accepted | 2ms | 4980 KiB | ||||
59 | Accepted | 2ms | 4956 KiB | ||||
60 | Wrong answer | 2ms | 5068 KiB |