Logo
(追記) (追記ここまで)

23507번 - Marbles 다국어

시간 제한메모리 제한제출정답맞힌 사람정답 비율
2 초 (추가 시간 없음) 128 MB513323.077%

문제

You are given a track of length $L$. On it there are $N$ marbles at positions $A_1,A_2\cdots,A_N$ and $N$ switches at positions $B_1,B_2,\cdots,B_N,ドル 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 1ドル$ 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 $i$ to travel to the position $i+1$ or $i-1,ドル and it also takes exactly one second to travel from 1ドル$ back to 1ドル$ while changing directions, or to travel from $L$ back to $L$ 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 $L$ ($L\le 10^9$) and $N$ ($N\le3000$)

In the second line of input you are given $A_1,A_2\cdots,A_N,ドル the initial positions of the marbles. All $A_i$ are guaranteed to be distinct, and 1ドル\le A_i\le L$.

In the second line of input you are given $B_1,B_2\cdots,B_N,ドル the initial positions of the marbles. All $B_i$ are guaranteed to be distinct, and 1ドル\le B_i\le L$.

출력

Print one integer: the minimum amount of time to have a marble on top of every switch. If solution does not exist, print $-1$.

제한

예제 입력 1

4 2
2 4
1 4

예제 출력 1

1

힌트

출처

Contest > Open Cup > 2019/2020 Season > Stage 19: The Grand Prix of Serbia I번

(追記) (追記ここまで)

출처

대학교 대회

  • 사업자 등록 번호: 541-88-00682
  • 대표자명: 최백준
  • 주소: 서울시 서초구 서초대로74길 29 서초파라곤 412호
  • 전화번호: 02-521-0487 (이메일로 연락 주세요)
  • 이메일: contacts@startlink.io
  • 통신판매신고번호: 제 2017-서울서초-2193 호

AltStyle によって変換されたページ (->オリジナル) /