#P1798. 小明搬家
小明搬家
Description
Xiaoming is moving, and everyone comes to help.
Xiaoming now lives on floor . In total, people need to carry large boxes up to floor .
At the start, all boxes are on floor . After a period of chaotic moving, things got messy. Everyone then realized the chaos was inefficient and agreed on a more efficient method:
- Each person moves at a speed of one floor per minute, either up or down.
- Everyone going up carries exactly one box; everyone going down carries no box.
- Upon reaching floor , a person immediately puts the box down and starts going down. Upon reaching floor , a person immediately picks up a box and starts going up.
- When a person going up meets a person going down on the stairs, the up-going person hands the box to the other person, and both reverse direction simultaneously. That is, the person who was carrying a box upward now goes downward without a box, and the person who was going downward without a box now goes upward carrying the box.
Find the minimum time needed to finish moving all the boxes.
Input Format
The first line contains , denoting the number of floors, the number of people, and the number of boxes still lying on floor .
The next lines each contain two integers .
is the current floor of the -th person. is or : means the -th person is carrying a box and going up, and means the -th person is going down without a box.
It is guaranteed that no two people are on the same floor at the same time; anyone on floor must be going up with a box, and anyone on floor must be going down without a box.
Output Format
Output a single integer: the time to finish moving all the boxes.
5 2 4
1 0
3 0
20
Hint
- For of the testdata, , .
- For of the testdata, , .
- For of the testdata, , , .
Translated by ChatGPT 5
京公网安备 11011102002149号