Traffic Lights
InterviewTime limit1sMemory limit2048 MB
Given n traffic lights with red and green durations that all turn red together, find the earliest time when every light is green again, or -1 if that never happens.
- Level
Medium4 of 10
- Topics
- Simulation, Number theory
- Solved
- No attempts yet
Problem
A street has traffic lights. Each light alternates between seconds of red and seconds of green. The values of and can differ from light to light.
All the lights were green. Then all of them turned red at the same moment. Find how many seconds it takes until every light is green again.
Input
The first line contains a single integer (), the number of traffic lights. Each of the next lines contains two integers and (), the number of seconds that light stays red and green.
Output
Output a single integer, the earliest time at which all the lights are green again. If this never happens, output .