#include <cmath>
#include <vector>
#include <algorithm>
#include <iostream>
using ll = long long;
using namespace std;
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) * pow(bs, MAXD - i - 2));
else
{
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;
}
if(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;
}
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) * 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;
}
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 | 2008 KiB | ||||
3 | Accepted | 3ms | 2248 KiB | ||||
subtask2 | 0/25 | ||||||
4 | Accepted | 3ms | 2468 KiB | ||||
5 | Wrong answer | 3ms | 2744 KiB | ||||
6 | Accepted | 3ms | 2656 KiB | ||||
7 | Accepted | 3ms | 2660 KiB | ||||
8 | Accepted | 3ms | 2788 KiB | ||||
9 | Accepted | 3ms | 2996 KiB | ||||
10 | Wrong answer | 3ms | 3084 KiB | ||||
11 | Accepted | 3ms | 3328 KiB | ||||
12 | Accepted | 3ms | 3452 KiB | ||||
13 | Accepted | 3ms | 3596 KiB | ||||
14 | Accepted | 3ms | 3856 KiB | ||||
15 | Wrong answer | 3ms | 3712 KiB | ||||
16 | Accepted | 3ms | 3968 KiB | ||||
17 | Accepted | 3ms | 4144 KiB | ||||
18 | Accepted | 2ms | 4164 KiB | ||||
19 | Wrong answer | 2ms | 4312 KiB | ||||
subtask3 | 10/10 | ||||||
20 | Accepted | 3ms | 4476 KiB | ||||
21 | Accepted | 2ms | 4448 KiB | ||||
22 | Accepted | 2ms | 4452 KiB | ||||
23 | Accepted | 2ms | 4452 KiB | ||||
24 | Accepted | 3ms | 4572 KiB | ||||
subtask4 | 15/15 | ||||||
25 | Accepted | 3ms | 4568 KiB | ||||
26 | Accepted | 3ms | 4840 KiB | ||||
27 | Accepted | 2ms | 4780 KiB | ||||
28 | Accepted | 3ms | 4772 KiB | ||||
29 | Accepted | 3ms | 4776 KiB | ||||
30 | Accepted | 2ms | 4888 KiB | ||||
31 | Accepted | 2ms | 4892 KiB | ||||
32 | Accepted | 3ms | 4784 KiB | ||||
33 | Accepted | 2ms | 4788 KiB | ||||
subtask5 | 0/20 | ||||||
34 | Accepted | 3ms | 4784 KiB | ||||
35 | Accepted | 3ms | 4796 KiB | ||||
36 | Wrong answer | 3ms | 4900 KiB | ||||
37 | Wrong answer | 3ms | 4676 KiB | ||||
38 | Accepted | 3ms | 4696 KiB | ||||
39 | Accepted | 2ms | 4776 KiB | ||||
40 | Accepted | 2ms | 4776 KiB | ||||
41 | Accepted | 2ms | 4780 KiB | ||||
42 | Accepted | 2ms | 4780 KiB | ||||
subtask6 | 0/30 | ||||||
43 | Wrong answer | 3ms | 4668 KiB | ||||
44 | Wrong answer | 3ms | 4672 KiB | ||||
45 | Wrong answer | 2ms | 4784 KiB | ||||
46 | Accepted | 2ms | 4784 KiB | ||||
47 | Accepted | 2ms | 4676 KiB | ||||
48 | Wrong answer | 3ms | 4696 KiB | ||||
49 | Accepted | 3ms | 4668 KiB | ||||
50 | Accepted | 3ms | 4808 KiB | ||||
51 | Accepted | 2ms | 4888 KiB | ||||
52 | Accepted | 2ms | 4908 KiB | ||||
53 | Accepted | 2ms | 4904 KiB | ||||
54 | Accepted | 3ms | 4900 KiB | ||||
55 | Accepted | 3ms | 4932 KiB | ||||
56 | Accepted | 3ms | 4904 KiB | ||||
57 | Accepted | 3ms | 4908 KiB | ||||
58 | Accepted | 3ms | 4904 KiB | ||||
59 | Accepted | 3ms | 4908 KiB | ||||
60 | Wrong answer | 3ms | 5016 KiB |