#P2070. Barn Painting with Overlapping Layers

    ID: 15352 Type: Default 1000ms 256MiB

Barn Painting with Overlapping Layers

Barn Painting with Overlapping Layers

Farmer John has devised a method to decorate the long fence beside his barn (consider the fence as a one‐dimensional line). He ties a paintbrush to his favorite cow Bessie and then goes off to enjoy a cold drink. While Bessie walks back and forth across the fence, every time she passes over a section of the fence that segment gets painted.

Bessie starts at position 0 and performs a sequence of N moves (1 \(\le N \le 100000\)). Each move is given as an integer distance along with a direction. For example: 10 L means Bessie moves 10 units to the left, and 15 R means she moves 15 units to the right. As she moves, each segment on the fence gets an additional coat of paint.

Farmer John wants to know the total length of the fence which has received at least two coats of paint (since areas painted only once may be washed off by rain). Note that the distances can be very large (up to 1,000,000,000 units away from the start).

inputFormat

The input starts with a single integer N (1 \(\le N \le 100000\)), the number of moves. Each of the next N lines contains an integer and a character separated by a space. The integer represents the distance and the character is either 'L' or 'R', indicating a move to the left or right, respectively. Bessie always starts at position 0.

outputFormat

Output a single integer representing the total length of the fence that has been painted with at least two coats of paint.

sample

4
10 R
3 L
15 R
10 L
15

</p>