atcoder#AGC022D. [AGC022D] Shopping

[AGC022D] Shopping

Score : 16001600 points

Problem Statement

Yui loves shopping. She lives in Yamaboshi City and there is a train service in the city. The city can be modelled as a very long number line. Yui's house is at coordinate 00.

There are NN shopping centres in the city, located at coordinates x1,x2,...,xNx_{1}, x_{2}, ..., x_{N} respectively. There are N+2N + 2 train stations, one located at coordinate 00, one located at coordinate LL, and one located at each shopping centre.

At time 00, the train departs from position 00 to the positive direction. The train travels at a constant speed of 11 unit per second. At time LL, the train will reach the last station, the station at coordinate LL. The train immediately moves in the opposite direction at the same speed. At time 2L2L, the train will reach the station at coordinate 00 and it immediately moves in the opposite direction again. The process repeats indefinitely.

When the train arrives at a station where Yui is located, Yui can board or leave the train immediately. At time 00, Yui is at the station at coordinate 00.

Yui wants to go shopping in all NN shopping centres, in any order, and return home after she finishes her shopping. She needs to shop for tit_{i} seconds in the shopping centre at coordinate xix_{i}. She must finish her shopping in one shopping centre before moving to the next shopping centre. Yui can immediately start shopping when she reaches a station with a shopping centre and she can immediately board the train when she finishes shopping.

Yui wants to spend the minimum amount of time to finish her shopping. Can you help her determine the minimum number of seconds required to complete her shopping?

Constraints

  • 1N3000001 \leq N \leq 300000
  • 1L1091 \leq L \leq 10^{9}
  • 0<x1<x2<...<xN<L0 < x_{1} < x_{2} < ... < x_{N} < L
  • 1ti1091 \leq t_{i} \leq 10^{9}
  • All values in the input are integers.

Partial Score

  • 10001000 points will be awarded for passing the testset satisfying 1N30001 \leq N \leq 3000.

Input

Input is given from Standard Input in the following format:

NN LL

x1x_{1} x2x_{2} ...... xNx_{N}

t1t_{1} t2t_{2} ...... tNt_{N}

Output

Print the minimum time (in seconds) required for Yui to finish shopping at all NN shopping centres and return home.

2 10
5 8
10 4
40

Here's one possible way for Yui to finish her shopping :

  • Travel to the station at coordinate 88 with the train. She arrives at coordinate 88 at time 88.
  • Shop in the shopping centre at coordinate 88. She finishes her shopping at time 1212.
  • The train arrives at coordinate 88 at time 1212. She boards the train which is currently moving in the negative direction.
  • She arrives at coordinate 55 at time 1515. She finishes her shopping at time 2525.
  • The train arrives at coordinate 55 at time 2525. She boards the train which is currently moving in the positive direction.
  • She arrives at coordinate L=10L = 10 at time 3030. The train starts moving in the negative direction immediately.
  • She arrives at coordinate 00 at time 4040, ending the trip.
2 10
5 8
10 5
60
5 100
10 19 28 47 68
200 200 200 200 200
1200
8 1000000000
2018 123456 1719128 1929183 9129198 10100101 77777777 120182018
99999999 1000000000 1000000000 11291341 1 200 1 123812831
14000000000

Beware of overflow issues.