#P8041. Recursive Scale Balancing and Binary Tattoo
Recursive Scale Balancing and Binary Tattoo
Recursive Scale Balancing and Binary Tattoo
Today, Kile celebrates his birthday. His best friend Ivan gifts him a recursive scale. In this scale, each beam can end with either a fixed weight (a positive integer), a new scale, or nothing (represented by 0). The scale tilts to the left if the total mass on its left beam is greater than that on its right beam, tilts to the right if the right mass is larger, and is considered balanced only when the two sides are exactly equal and all its subscales are balanced.
Being a true computer scientist, Kile decides to balance this gift by adding a new weight, with the smallest possible positive real mass, at the unique empty position marked by a question mark (?
). Once the minimal new weight is added so that every scale (and subscale) balances, Kile tattoos his chest with the total mass of all weights on the balanced scale. This mass is represented in binary without leading zeroes. It can be proven that the total mass will be a positive integer.
The structure of the scale is described by a recursive grammar:
scale ::= "[" item "," item "]" item ::= positive_integer | "0" | "?" | scale
It is guaranteed that there is exactly one occurrence of ?
in the input and that there exists a solution for balancing.
inputFormat
A single line containing the recursive representation of the scale. The format follows the grammar:
scale ::= "[" item "," item "]" item ::= positive_integer (e.g., 1, 2, 10) | "0" (denoting an empty end) | "?" (denoting the unique position to add the new weight) | scale
There is exactly one occurrence of "?" in the input.
outputFormat
Output the binary representation (without leading zeros) of the total mass of all weights on the balanced scale after adding the minimal required weight.
sample
[1,?]
10