#P7022. Marble Splitting Game

    ID: 20229 Type: Default 1000ms 256MiB

Marble Splitting Game

Marble Splitting Game

Debbie, Debby, Debra, and Deborah have gathered their marbles to play a cooperative game. They originally contribute $2^{d_1}$, $2^{d_2}$, $2^{d_3}$, and $2^{d_4}$ marbles respectively. All the marbles are combined into a single pile of $N=2^{d_1}+2^{d_2}+2^{d_3}+2^{d_4}$ marbles. The game then proceeds in turns. In each turn the kids perform the following two steps:

  1. They choose any pile having at least 2 marbles and split it into two non-empty piles. That is, if the chosen pile contains $m\ge2$ marbles, they split it into two piles containing $m_1$ and $m_2$ marbles respectively, with $m_1+m_2=m$ and both $m_1$ and $m_2$ positive.
  2. After the split, if there exist several piles having the same number of marbles, only one of them is kept while all the others are discarded.

The game ends when there is exactly one pile remaining and that pile contains one marble. The goal is to finish the game in as few turns as possible. It can be shown that an optimal strategy is as follows:

  • If the current pile size $m$ is even, split it evenly into two piles of size $\frac{m}{2}$. Since these two piles are equal, step 2 eliminates one, effectively reducing the pile to size $\frac{m}{2}$ in one turn.
  • If $m$ is odd and $m>1$, any split yields two different positive parts. In an optimal play the best option is to choose the split 1 and $m-1$. Then the game continues on two piles of sizes 1 and $m-1$. Note that later when two piles of 1 appear they merge automatically without an extra turn. Hence, in this case the minimal extra turns required is just 1 (for the split) plus the number of turns needed to reduce the $m-1$ pile.

Thus, if we define a function $T(m)$ as the minimal number of turns required to reduce a pile of $m$ marbles to a single marble, we have:

[ T(m)=\begin{cases} 0,& m=1,\ 1+T\Big(\frac{m}{2}\Big),& m\text{ is even},\ 1+T(m-1),& m\text{ is odd and }m>1. \end{cases} ]

Given d1, d2, d3, and d4, compute the minimal number of turns needed.

inputFormat

The input consists of a single line containing four non‐negative integers d1, d2, d3, and d4, separated by spaces.

It is guaranteed that the initial pile size $N=2^{d_1}+2^{d_2}+2^{d_3}+2^{d_4}$ is greater than 1.

outputFormat

Output a single integer — the minimal number of turns required to end the game.

sample

1 1 1 1
3