QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 32 MB Total points: 100

#12979. Printed-Circuit Boards

الإحصائيات

The firm of Bytel starts to produce series-parallel electronic circuits. Each such a circuit consists of electronic units, connections between them, and two power connections. A series-parallel circuit may consist of:

a single unit

problem_12979_1.jpg

several smaller series-parallel circuits connected in series

problem_12979_2.jpg

two branching units connecting in parallel several smaller series-parallel circuits.

problem_12979_3.jpg

The circuits are assembled on two-sided printed-circuit boards. The problem is to determine which connections should run on the top and which on the bottom side of the board. For technical reasons as many connections as possible should run on the bottom side but to each unit at least one must come from the top side of the board.

Write a program which:

  • reads the description of a series-parallel circuit,
  • computes the minimal number of connections that must run on the top side of the board,
  • writes the result.

Input

From the standard input one should read the description of a series-parallel circuit. The description is in a recursive form:

  • if the first line of the description contains a capital letter S and a positive integer $n$ ($2 ≤ n ≤ 10\,000$) separated from each other by a single space, then the circuit being described consists of n smaller circuits connected in series; they are described in the successive lines,
  • if the first line of the description contains a capital letter R and a positive integer $n$ ($2 ≤ n ≤ 10\,000$) separated from each other by a single space, then the circuit being described consists of smaller circuits connected in parallel (by means of two branching units); they are described in the successive lines,
  • a line containing only one capital letter X describes a circuit consisting of a single unit only.

The total number of letters X occurring in the description of a circuit does not exceed $10\,000\,000$, and the recursive depth of the description does not exceed $500$.

Output

Your program should write to the standard output. In the first line there should be one integer equal to the minimal number of connections that must run on the top side of the board.

Example

Input

R 3
S 2
X
R 2
S 2
X
X
S 2
X
X
S 3
X
X
X
R 2
X
X

Output

8
problem_12979_4.jpg

Scheme of the series-parallel circuit for the data from the example. Solid lines indicate the connections running on the top side of the board.

About Issues

We understand that our problem archive is not perfect. If you find any issues with the problem, including the statement, scoring configuration, time/memory limits, test cases, etc.

You may use this form to submit an issue regarding the problem. A problem moderator will review your issue and proceed it properly.

STOP! Before you submit an issue, please READ the following guidelines:

  1. This is not a place to publish a discussion, editorial, or requests to debug your code. Your issue will only be visible by you and problem moderators. Other users will not be able to view or reply your issues.
  2. Do not submit duplicated issues. If you have already submitted one, please wait for an moderator to review it. Submitting multiple issues will not speed up the review process and might cause your account to be banned.
  3. Issues must be filed in English or Chinese only.
  4. Be sure your issue is related to this problem. If you need to submit an issue regarding another problem, contest, category, etc., you should submit it to the corresponding page.

Active Issues 0

No issues in this category.

Closed/Resolved Issues 0

No issues in this category.