Under construction

Still being built. Things may change

Quality Expressions

Time limit1sMemory limit64 MB

Summary
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 Z1Z_1 is a quality expression. Such an expression has the form (x)(x). For example, if Z1=5Z_1 = 5, then (4)(4) is a quality expression.
  • If A1,A2,…,AkA_1, A_2, \ldots, A_k are quality expressions, 2≤k≤K2 \le k \le K, and the sum of their values is at most ZkZ_k, then the following two expressions are quality expressions as well.

(A1+A2+⋯+Ak)(A1∗A2∗⋯∗Ak)(A_1 + A_2 + \cdots + A_k) \qquad (A_1 * A_2 * \cdots * A_k)

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 KK. (2≤K≤502 \le K \le 50)

The second line contains the integers Z1,…,ZKZ_1, \ldots, Z_K, separated by spaces. (1≤Zi≤501 \le Z_i \le 50)

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 K=2K = 2, Z1=10Z_1 = 10, Z2=6Z_2 = 6, and the expression ((?)+(?)). Replacing both question marks by 3 gives ((3)+(3)), where the two subexpressions have values summing to 66, which is at most Z2Z_2, so it is a quality expression and its value is 66. No larger value is possible.

Examples3

  1. Example 1

    Input
    2
    10 6
    ((?)+(?))
    
    Expected output
    6.000000
    
  2. Example 2

    Input
    3
    2 5 3
    (((?)+(?))*(?))
    
    Expected output
    6.000000
    
  3. Example 3

    Input
    3
    2 10 6
    ((?)*(?)*(?))
    
    Expected output
    8.000000