Marbles
시간 제한2초메모리 제한128 MB
선분 위에서 구슬이 튕기며 움직일 때, 모든 스위치가 동시에 구슬로 덮이는 최소 시간을 구하거나 -1을 출력한다.
문제
You are given a track of length . On it there are marbles at positions and switches at positions , both of negligably small size. In the beginning, you direct each marble left or right and they all start moving at the constant speed of in the given direction. When two marbles collide, they bounce off each other elastically, meaning that they continue moving in opposite directions at the same speed. If a marble collides with the beginning or end of the track it bounces off with the same speed in the opposite direction. Keep in mind that it takes exactly one second for a marble at position to travel to the position or , and it also takes exactly one second to travel from back to while changing directions, or to travel from back to while changing directions. Note that marbles never stop on the switches.
The goal is to have a marble on top of every switch, i.e. to have all marbles simultaneously on the corresponding switches.
You need to find the minimum amount of time needed to achieve this.
입력
In the first line of input you are given () and ()
In the second line of input you are given , the initial positions of the marbles. All are guaranteed to be distinct, and .
In the second line of input you are given , the initial positions of the marbles. All are guaranteed to be distinct, and .
출력
Print one integer: the minimum amount of time to have a marble on top of every switch. If solution does not exist, print .