Rhythm Game of Balls
Time limit1sMemory limit1024 MB
Given N balls on a line that are stationary or moving left or right, find the total points scored from collisions until none occur.
Problem
A line of infinite length has balls on it. Each ball is either stationary, moves to the left, or moves to the right. At the start, all ball positions are different.
Hyeonje makes rhythm games and noticed a steady beat in the way moving balls hit each other. The game starts at 0 points. Each time balls collide, points are scored and the ball states change as follows.
- When a ball moving left and a ball moving right collide, both reverse their directions. This scores 1 point.

- When a moving ball hits a stationary ball, the moving ball stops at that spot, and the stationary ball starts moving in the direction the moving ball was going. This scores 2 points.

- When a ball moving left, a stationary ball, and a ball moving right collide at the same time, the left-moving and right-moving balls both reverse direction, and the stationary ball stays where it is. This scores 5 points.

If one collision meets several conditions, only the condition with the highest score applies.
Find the total score after enough time has passed that no more collisions occur. The total score is the sum of all points earned.
Every moving ball keeps a speed of 1, and a ball that starts moving after a collision also has speed 1. Balls are assumed to be small enough that their length and volume are negligible. Apart from collisions, each ball keeps its direction and speed.
Input
The first line contains the number of balls .
Each of the next lines contains an integer and an integer , separated by a space. ,
is the coordinate of the ball on the line. gives the initial state as follows.
- : the ball is stationary.
- : the ball moves to the left.
- : the ball moves to the right.
All ball positions are different.
Output
Print the total score after enough time has passed that no more collisions occur.