Camp Circular Bus
InterviewTime limit1sMemory limit1024 MB
Find when the last soldier alights from a circular bus that never stops, where each soldier boards at the first bus pass after their wait starts.
- Level
Medium4 of 10
- Topics
- Implementation, Math
- Solved
- No attempts yet
Problem
There are boarding and alighting stops in the camp, numbered from to . On a clear morning, the driver starts the bus again to help soldiers move around the camp.
The driver's bus is called the camp circular bus and has the following features.
- The camp circular bus leaves stop at time , visits stops , , , in order, then returns to stop . It keeps repeating this route.
- After departure, the bus cannot stop until all soldiers have boarded and alighted.
- The bus has no capacity limit. A soldier who wants to board can always get on.
A soldier starts waiting for the bus at a time, boards the moment the bus reaches the boarding stop, and alights the moment the bus reaches the alighting stop.
The driver helped all soldiers move and finished work as soon as the last soldier alighted. Find the time when the driver finished work.
Input
The first line contains the number of stops and the number of soldiers , separated by a space.
The second line contains integers , separated by spaces, which is the time to travel from stop to stop . Here is the time to return from stop to stop .
From the third line through line , each line gives one soldier: the boarding stop , the alighting stop , and the time when the soldier started waiting for the bus at the boarding stop, separated by spaces.
Output
Print one line with the time when the driver finished work.