eolymp
bolt
Try our new interface for solving problems
Məsələlər

Lightbulb Testing

Lightbulb Testing

Light bulbs all claim a lifetime in hours, but that's assuming the bulb is on continuously. Most people turn their light bulbs on and off. How does this affect their lifetime? "The Pumpkin King wants to review various lightbulb manufacturers for the glowing red eyes on this year's newest toy, "The Exorcist Barbie", so he's ordered Dr. Finklestein to build a rig to test this. Given a simple pattern, it'll 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 an element, or a repeating list. A repeating list consists of a list of elements, surrounded by parentheses, and follows by an asterisk, '\textbf{*}', and then an integer \textbf{k} (\textbf{1} ≤ \textbf{k} ≤ \textbf{1000000}). This integer \textbf{k} indicates how many times the list should be repeated. The Mad Doctor's machine always starts by turning the bulb on, and then goes through the pattern, for every integer, it waits that number of (time units), and then switches the bulb. If the bulb is on, it's turned off, and if it's off, it gets turned on. If the machine reaches the end of the pattern, it will start again from the beginning. For example, with this pattern: \textbf{1 3 5} The machine will start by turning the bulb on. \begin{itemize} \item It will wait \textbf{1} (time unit) and turn the bulb \textbf{off}. \item It will then wait \textbf{3} (units) and turn the bulb \textbf{on}. \item It will then wait \textbf{5} (units) and turn the bulb \textbf{off}. \item It will then wait \textbf{1} (unit) and turn the bulb \textbf{on}. \item It will then wait \textbf{3} (unit3) and turn the bulb \textbf{off}. \item It will continue like this until the bulb burns \textbf{out}. \end{itemize} Here's an example of a pattern with a repeating section: \textbf{1 (3 5)*2 7} The machine will start by turning the bulb on, and will change the bulbs state after \textbf{1}, then \textbf{3}, then \textbf{5}, then \textbf{3}, then \textbf{5}, then \textbf{7}, then \textbf{1}..... note that repeating sections can be nested inside of repeating sections. The Doctor 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 burn out - but, how long should it have taken? Assume that turning the bulb on and off does NOT affect it's life. Given a life n in hours, and a pattern of turning the bulb on and off, how many hours elapse before the bulb gets n hours of "\textbf{on}" time? \InputFile There will be multiple data sets in the input. Each data set will consist of two lines. The first line will be an integer \textbf{n} (\textbf{1}≤ \textbf{n} ≤ \textbf{1000000000}) which is the life of the bulb in hours. On the next line will be a pattern, as specified above. There will be spaces separating elements of a list, but nowhere else. Input ends with a zero in the leftmost position. \OutputFile A single integer, indicating the number of hours until the bulb has been lit for \textbf{n} hours.
Zaman məhdudiyyəti 3 saniyə
Yaddaşı istafadə məhdudiyyəti 256 MiB
Giriş verilənləri #1
100
20 30 40
1000
1 3 5
1000
1 (3 (5)*3 2)*2 7
0
Çıxış verilənləri #1
190
1999
2279
Mənbə ACM ICPC North America - South Central USA 2011