Gahee and the Crossing That Never Opens
Time limit1sMemory limit1024 MB
Given arrival times of upbound and downbound trains, find how many seconds the crossing barrier stays up. Each train blocks the crossing for 40 seconds.
- Level
Easy3 of 10
- Topics
- Intervals, Sorting, Implementation
- Solved
- No attempts yet
Problem
A railroad crossing is where a railway track meets a road. When a train passes through a crossing, the barrier goes down so that cars and people cannot cross.
Some crossings rarely open because trains pass through them often. Seosomun crossing near Seoul Station and Shinagawa crossing on the Keikyu Main Line are well-known examples.
Gahee lives in a town with one crossing, the Sanseongdae-ro crossing. A single route with a double-track line runs through the Sanseongdae-ro crossing, and both upbound and downbound trains approach it. The crossing works as follows.
- If no train is passing through the Sanseongdae-ro crossing, the barrier goes up. Otherwise, the barrier goes down.
- When a train approaches the Sanseongdae-ro crossing, it fully clears the crossing 40 seconds later. For example, suppose a train approaches at 11:10:15.
- From 11:10:15 until 11:10:55, the train is passing through the crossing.
- At 11:10:55, the train has fully cleared the crossing.
Given the times at which upbound and downbound trains approach the Sanseongdae-ro crossing during one day, find how many seconds the barrier of the Sanseongdae-ro crossing is up.
Input
The first line contains the number of upbound trains c and the number of downbound trains h.
The next c lines contain the times at which upbound trains approached the crossing, in hh:mm:ss format, in the order they arrived.
The next h lines contain the times at which downbound trains approached the crossing, in hh:mm:ss format, in the order they arrived.
Output
Print the answer.
Constraints
- Trains in the same direction are at least 60 seconds apart.
- No trains other than the given ones approach the Sanseongdae-ro crossing.
- All trains in the same direction use the same track.
- Trains in opposite directions use different tracks.
- All given times are after 05:00:00 and before 23:59:01.
- Leap seconds are ignored.
Hint
A double track is a line made of two tracks. In this problem, a double track means a line with one track for upbound trains and one track for downbound trains.