CF981F.Round Marriage

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

It's marriage season in Ringland!

Ringland has a form of a circle's boundary of length LL . There are nn bridegrooms and nn brides, and bridegrooms decided to marry brides.

Of course, each bridegroom should choose exactly one bride, and each bride should be chosen by exactly one bridegroom.

All objects in Ringland are located on the boundary of the circle, including the capital, bridegrooms' castles and brides' palaces. The castle of the ii -th bridegroom is located at the distance aia_i from the capital in clockwise direction, and the palace of the ii -th bride is located at the distance bib_i from the capital in clockwise direction.

Let's define the inconvenience of a marriage the maximum distance that some bride should walk along the circle from her palace to her bridegroom's castle in the shortest direction (in clockwise or counter-clockwise direction).

Help the bridegrooms of Ringland to choose brides in such a way that the inconvenience of the marriage is the smallest possible.

输入格式

The first line contains two integers nn and LL ( 1n21051 \leq n \leq 2 \cdot 10^{5} , 1L1091 \leq L \leq 10^{9} ) — the number of bridegrooms and brides and the length of Ringland.

The next line contains nn integers a1,a2,,ana_1, a_2, \ldots, a_n ( 0ai<L0 \leq a_i < L ) — the distances from the capital to the castles of bridegrooms in clockwise direction.

The next line contains nn integers b1,b2,,bnb_1, b_2, \ldots, b_n ( 0bi<L0 \leq b_i < L ) — the distances from the capital to the palaces of brides in clockwise direction.

输出格式

In the only line print the smallest possible inconvenience of the wedding, where the inconvenience is the largest distance traveled by a bride.

输入输出样例

  • 输入#1

    2 4
    0 1
    2 3
    

    输出#1

    1
    
  • 输入#2

    10 100
    3 14 15 92 65 35 89 79 32 38
    2 71 82 81 82 84 5 90 45 23
    

    输出#2

    27
    

说明/提示

In the first example the first bridegroom should marry the second bride, the second bridegroom should marry the first bride. This way, the second bride should walk the distance of 11 , and the first bride should also walk the same distance. Thus, the inconvenience is equal to 11 .

In the second example let pip_i be the bride the ii -th bridegroom will marry. One of optimal pp is the following: (6,8,1,4,5,10,3,2,7,9)(6,8,1,4,5,10,3,2,7,9) .

首页