#P5756. TB Language Program Statement Execution Count

    ID: 18984 Type: Default 1000ms 256MiB

TB Language Program Statement Execution Count

TB Language Program Statement Execution Count

This problem involves simulating a program written in the Tiny Basm language (TB language). The TB language has the following types of statements:

  • Accumulation statement: In the format L X+K, where L is the line number, X is a variable name (an uppercase letter), and K is an integer. This statement adds K to variable X. The integer K can be arbitrarily large.
  • Output statement: In the format L ?X, which outputs the value of variable X to the monitor (for simulation purposes you only need to count the statement execution).
  • Conditional statement: In the format L IF X=K GO M, where the statement checks if the variable X equals the integer K (with K having at most 4 digits). If the condition is true, then control jumps to the statement with line number M. Regardless of whether the condition is satisfied, this statement is counted as executed exactly once.
  • End statement: In the format L END. When an END statement is executed, the program terminates.

Execution rules:

  • The program starts execution at the statement with the smallest line number.
  • If no jump occurs, the statements are executed in increasing order of line number.
  • All variables are automatically initialized to $0$ before execution.
  • An executed conditional statement is counted even if its condition is false.
  • After a jump, execution continues from the target statement. If there is no next statement in order after execution, the program terminates.

Your task is to simulate the execution of a given TB language program P and determine the total number of statements executed before the program terminates.

inputFormat

The input starts with a single integer n indicating the number of lines in the program. Each of the following n lines contains a valid TB language statement. The statements may come in any order; however, the control flow is determined by the line numbers.

It is guaranteed that all statements are valid and no error statements occur.

Examples of statements include:

  • 10 A+5 (accumulation statement)
  • 20 ?A (output statement)
  • 30 IF A=5 GO 50 (conditional statement)
  • 40 END (end statement)

outputFormat

Output a single integer which is the total number of statements executed during the simulation of the TB language program.

sample

3
10 A+5
20 ?A
30 END
3