Quality Expressions
Time limit1sMemory limit64 MB
Given a bracket expression with ? placeholders and per-arity limits, choose values so the expression is valid and its value is as large as possible.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Tree, Backtracking
- Solved
- No attempts yet
Problem
A quality expression is built from brackets, numbers, multiplication, and addition.
Quality expressions are defined recursively.
- An expression that consists of a single positive real number at most is a quality expression. Such an expression has the form . For example, if , then is a quality expression.
- If are quality expressions, , and the sum of their values is at most , then the following two expressions are quality expressions as well.
You are given a quality expression in which every number has been replaced by a question mark. Find the maximal value the original expression could have had.
Input
The first line contains an integer . ()
The second line contains the integers , separated by spaces. ()
The third line contains one quality expression in the format described above. The expression consists only of the five characters ?, *, +, (, ), and its length is at most 1,000,000.
Output
Print the maximal value of the expression on one line, rounded to exactly six decimal places.
Note
Take , , , and the expression ((?)+(?)). Replacing both question marks by 3 gives ((3)+(3)), where the two subexpressions have values summing to , which is at most , so it is a quality expression and its value is . No larger value is possible.