Both Sides
Natasha has arrived at the basketball match just in time to discover that she does not know which team her friends are supporting. Too embarrassed to ask, she decides to cheer whenever either team takes the lead. Surely nobody will notice if she sounds enthusiastic enough.
There are two teams, numbered 1 and 2, and both start with zero points. During the match, there are N scoring events. In event i, team Ti earns Pi points, which are added to its score. The events occur in the order given.
Natasha cheers once after a scoring event if the team that scored was tied with or behind the other team immediately before the event, and is strictly ahead immediately afterwards. She does not cheer when a team merely extends its lead or brings the scores level.
How many times does Natasha cheer during the match?
Input
The first line contains an integer N (1≤N≤1000), the number of scoring events.
Each of the next N lines contains two integers Ti and Pi (Ti∈{1,2}, Pi∈{1,2,3}), the team that scores and the number of points it earns in event i. The events are listed in chronological order.
Output
Print a single integer: the number of times Natasha cheers.
Example 1
8
1 2
1 1
2 3
2 1
1 2
2 1
1 1
2 3
5
Explanation
Natasha cheers after events 1, 4, 5, 7, and 8. Event 2 only extends a lead, while events 3 and 6 bring the scores level.
Example 2
1
2 3
1
Explanation
Both teams start tied at zero. Team 2 takes the lead with the only scoring event.
Example 3
4
1 1
2 1
1 1
2 1
2
Explanation
Natasha cheers after events 1 and 3. Team 1 takes the lead twice, even though team 2 never leads. The match ends in a tie.
Comments0
No comments yet
Be the first to comment.
New comment
Log in to join the discussion.