Whenever you buy a light bulb, the package will state an amount of time that the bulb should last. But, that's assuming the bulb is on continuously. Most people turn their light bulbs on and off. How does this affect their lifetime?
A friend of yours has built a rig to test this. Given a simple pattern, his rig will turn the bulb on and off, and a light sensor will tell when the bulb burns out. The pattern has a very simple syntax. It consists of a list of elements, separated by spaces. Each element is either a number M (1 ≤ M ≤ 1,000,000), indicating a number of milliseconds, or a repeating list. A repeating list consists of one or more elements, surrounded by parentheses, and followed by a ‘*’ and then an integer K (1 ≤ K ≤ 100). This integer K indicates how many times the list should be repeated. Your friend’s machine always starts with the bulb on, and then goes through the pattern. For every integer, it waits that number of milliseconds, and then switches the bulb. If the bulb is on, it's turned off, and if it's off, it gets turned back on. If the machine reaches the end of the pattern, it will start again from the beginning.
For example, with this pattern:
1 3 5
The machine will start by turning the bulb on.
It will wait 1 millisecond and turn the bulb off.
It will then wait 3 milliseconds and turn the bulb on.
It will then wait 5 milliseconds and turn the bulb off.
It will then wait 1 millisecond and turn the bulb on.
It will then wait 3 milliseconds and turn the bulb off.
It will continue like this until the bulb burns out.
Here's an example of a pattern with a repeating section:
1 (3 5)*2 7
The machine will start by turning the bulb on, and will change the bulb’s state after 1 millisecond, then 3, then 5, then 3, then 5, then 7, then 1.....
Note that repeating sections can be nested inside of repeating sections.
Your friend is not good with programming, though, so he's asked you to help. He can easily measure how long it takes for the bulb to actually burn out - but, how long SHOULD it have taken? Assume that turning the bulb on and off does NOT affect its life. Also assume that changing the bulb’s state takes no time.
Given a life N in milliseconds, and a pattern of turning the bulb on and off, how many actual milliseconds would elapse before the bulb is on for exactly N milliseconds?