No.808 Kaiten Sushi?
タグ : / 解いたユーザー数 55
作問者 :
sinsincoscos
/ テスター :
noshi91
Problem Statement
umg has come to Kaiten Sushi, a restaurant that opened in his neighborhood today. He seems to be the very first to arrive, and there are no other customers. Kaiten Sushi has a circular counter whose circumference is $L$ meters.
After entering, umg was shown to a seat. On the counter in the restaurant there are $N$ pieces of sushi and $N$ cups of tea. Sushi $i$ is located $x_i$ meters clockwise from umg,
and tea $j$ is located $y_j$ meters from him. No two or more sushi pieces or teas exist at the same position.
When umg calls out "start", the seat he is sitting on begins moving clockwise along the counter at a speed of $1$ meter per second. When he reaches the position of a piece of sushi, he can take and eat it. He does not have to take it.
Since umg is slow at swallowing, after eating $1$ piece of sushi he must drink $1$ cup of tea before eating the next piece of sushi. As with sushi, he can drink a cup of tea only upon reaching the position of that tea, but he does not have to drink it.
Once consumed, neither sushi nor tea is ever replenished. Since the counter is circular, the seat keeps going around along the counter until umg is satisfied.
umg consumes all $N$ pieces of sushi and all $N$ cups of tea, and when he drinks the $N$-th cup of tea, he pays and leaves the restaurant immediately. Find the minimum time from when he starts moving until he leaves the restaurant, when he consumes the sushi and tea appropriately. The time required for payment can be ignored.
Input
$N\ L$ $x_1\ x_2\ \cdots \ x_N$ $y_1\ y_2\ \cdots \ y_N$
$1\le N \le 10^5$
$2N\lt L \le 10^9$
$0 \lt x_1 \lt x_2 \lt \cdots \lt x_N \lt L$
$0 \lt y_1 \lt y_2 \lt \cdots \lt y_N \lt L$
$x_i,y_j$ are all distinct.
All values in the input are integers.
Output
Output the number representing the answer on one line. Print a newline at the end.
Sample
Sample 1
Input
3 7 1 2 4 3 5 6
Output
12
The minimum time can be achieved as follows.
・Eat the sushi at position 1.
・Drink the tea at position 3.
・Eat the sushi at position 4.
・Drink the tea at position 6.
・Eat the sushi at position 2.
・Drink the tea at position 5 and leave the restaurant.
Sample 2
Input
2 5 1 3 2 4
Output
4
He can consume the sushi and tea alternately, in order from the front.
Sample 3
Input
10 827 47 129 132 290 308 309 540 544 656 818 119 241 332 340 376 498 575 632 699 737
Output
3640
提出するには、Twitter 、GitHub、 Googleもしくは右上の雲マークをクリックしてアカウントを作成してください。