Resistor Circuit
Time limit1sMemory limit64 MB
Compute the equivalent resistance of a nested series-parallel resistor circuit described by a bracketed string.
Problem
Mirko got a "Young physicist" kit for his birthday. The kit holds types of resistors, connecting wires, and an ohmmeter. A resistor of type has a resistance of exactly ohms.
Resistors are connected in two ways, in series and in parallel. A whole circuit of resistors can be replaced by a single resistor whose resistance equals the equivalent resistance of that circuit. A series circuit is written as (R1-R2-R3-...-RK), and its equivalent resistance is
A parallel circuit is written as (R1|R2|R3|...|RK), and its equivalent resistance is
Mirko brought the kit to school and the teacher noticed it. She built one circuit and is asking Mirko for its equivalent resistance. Help him answer.
Input
The first line contains the number of resistor types . ()
The second line contains real numbers . ()
The third line contains the string describing the circuit. is at most 100,000 characters long and consists only of the characters R, the digits 1 to 9, (, ), -, and |. The number of bracket pairs is minimal and the brackets are paired correctly. No pair of brackets contains both - and |. The circuit uses only resistors from the kit. Some types may go unused, and a type may be used several times.
Output
Print the equivalent resistance of the circuit, rounded to exactly five decimal places. Pad with zeros so that five digits always follow the decimal point.