#P10132. Bouncing Cannonball

    ID: 12120 Type: Default 1000ms 256MiB

Bouncing Cannonball

Bouncing Cannonball

Bessie has mastered the art of transforming herself into a cannonball and bouncing along a number line of length $N$ ($1\le N\le 10^5$). The positions on the number line are numbered from $1$ to $N$. She starts at an integer position $S$ ($1\le S\le N$) with an initial energy of $1$ and an initial rightward direction.

At every integer position there is either a cannon target or a springboard. Each cannon target and each springboard has an integer value in the range $[0, N]$. When Bessie lands on a cannon target with value $v$, if her current energy is at least $v$, the target is immediately destroyed (if it has not been destroyed before). Landing on a cannon target does not change her energy or direction. In contrast, landing on a springboard with value $v$ increases her energy by $v$ and reverses her direction (from right to left or vice‐versa).

Note that if Bessie starts on a cannon target that she can destroy (i.e. her initial energy $\ge v$), it is immediately destroyed. Similarly, if she starts on a springboard, its effect applies before her first jump.

When Bessie has energy $k$, she bounces a distance of exactly $k$ in her current direction. That is, if her current position is $x$, her next position will be $x \pm k$, where the sign depends on her direction.

The simulation continues until Bessie eventually leaves the number line or loops infinitely. In either case, output the total number of cannon targets that Bessie destroys.

inputFormat

The first line contains two integers $N$ and $S$, representing the length of the number line and Bessie's starting position respectively.

Then follow $N$ lines. The $i$-th of these lines describes the object at position $i$ and contains a character and an integer separated by a space. The character is either T or S, where T represents a cannon target and S represents a springboard. The integer is the value associated with that target or springboard (in the range $[0, N]$).

It is guaranteed that every position from $1$ to $N$ has exactly one object.

outputFormat

Output a single integer — the total number of cannon targets destroyed by Bessie.

sample

5 3
T 1
S 2
T 1
T 3
S 0
1