When solving TopCoder problems, it is always important to make sure that a solution will not exceed the 8 second time limit. Sometimes this is as simple as just looking at the loops in the code and figuring out the maximum number of iterations through the inner most loop. If this number is sufficiently small, then it is obvious that the code will execute in time. However, for other cases, we must consider what the code is doing in the loops, as well as the number of times the loop is executed, to determine whether it will run in the given time frame..
You will be given a String[] containing loops and basic instructions. Loops will be represented as "for(<x>)" where <x> represents an integer and is the number of times the loop is executed. Basic instructions will be represented by the word "BASIC" followed by a semicolon. Loops will always have an accompanying set of braces, '{' and '}', to signify their scope. Thus "for(100){BASIC;BASIC;}" represents a loop which is executed 100 times, and contains two basic instructions. Your method should determine the total number of basic instruction executions in the program. So, for the above example, you would return 200.
Additionally, loops may be nested, just like they are in actual code. In the
code, "for(100){BASIC;for(12){BASIC;}}", the outer loop is executed 100 times,
and the inner loop is executed 12 times for each execution of the outer loop. Thus, the basic instruction in the
inner loop is executed 1200 times, and the basic instruction in the outer
loop is executed 100 times, for a total of 1300 basic instructions.
More formally, the input String[] will be a <statement> following the rules below. Note that these rules ignore spaces and line breaks because spaces and line breaks do not effect the semantics of the input.
<statement> ::= <statement><statement> | <for-loop> | <basic> | ""
<for-loop> ::= for(<iter>){<statement>}
<basic> ::= BASIC;
<iter> ::= <non-negative integer>
|