P1322 Logo Language

Description

Logo language commands can direct a turtle to crawl on the screen. This problem uses only three Logo statements: forward `FD`, backward `BK`, and repeat `REPEAT`, so the turtle moves back and forth along a single straight line. Given one line of a Logo command line, output the distance between the turtle and its original position (assume the screen is large enough to let the turtle move up to a distance of $10^9$). For example: - Input `FD 100`, output: $100$. - Input `FD 100 BK 150`, output: $50$. - Input `REPEAT 5[FD 100 BK 50]`, output: $250$. - Input `REPEAT 5[FD 50 REPEAT 10[FD 100]]`, output: $5250$.

Input Format

One line containing a string that is a valid Logo command line as specified above.

Output Format

A single number: the distance from the turtle’s original position.

Explanation/Hint

The absolute value of each statement’s argument does not exceed $1000$. The output will not exceed $10^9$. Translated by ChatGPT 5