Problem:
Score : 1600 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 0.

There are N shopping centres in the city, located at coordinates x_{1}, x_{2}, ..., x_{N} respectively. There are N + 2 train stations, one located at coordinate 0, one located at coordinate L, and one located at each shopping centre.

At time 0, the train departs from position 0 to the positive direction. The train travels at a constant speed of 1 unit per second. At time L, the train will reach the last station, the station at coordinate L. The train immediately moves in the opposite direction at the same speed. At time 2L, the train will reach the station at coordinate 0 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 0, Yui is at the station at coordinate 0.

Yui wants to go shopping in all N shopping centres, in any order, and return home after she finishes her shopping. She needs to shop for t_{i} seconds in the shopping centre at coordinate x_{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

1 \leq N \leq 300000

1 \leq L \leq 10^{9}

0 < x_{1} < x_{2} < ... < x_{N} < L

1 \leq t_{i} \leq 10^{9}

All values in the input are integers.

Partial Score

1000 points will be awarded for passing the testset satisfying 1 \leq N \leq 3000.

Input

Input is given from Standard Input in the following format:

N L
x_{1} x_{2} ... x_{N}
t_{1} t_{2} ... t_{N}

Output

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

Sample Input 1

2 10
5 8
10 4

Sample Output 1

40

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

Travel to the station at coordinate 8 with the train. She arrives at coordinate 8 at time 8.

Shop in the shopping centre at coordinate 8. She finishes her shopping at time 12.

The train arrives at coordinate 8 at time 12. She boards the train which is currently moving in the negative direction.

She arrives at coordinate 5 at time 15. She finishes her shopping at time 25.

The train arrives at coordinate 5 at time 25. She boards the train which is currently moving in the positive direction.

She arrives at coordinate L = 10 at time 30. The train starts moving in the negative direction immediately.

She arrives at coordinate 0 at time 40, ending the trip.

Sample Input 2

2 10
5 8
10 5

Sample Output 2

60

Sample Input 3

5 100
10 19 28 47 68
200 200 200 200 200

Sample Output 3

1200

Sample Input 4

8 1000000000
2018 123456 1719128 1929183 9129198 10100101 77777777 120182018
99999999 1000000000 1000000000 11291341 1 200 1 123812831

Sample Output 4

14000000000

Beware of overflow issues.

Solution in Rust (1.15.1) (judge verdict: Runtime Error)

// https://atcoder.jp/contests/agc022/tasks/agc022_d
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}


const INF: i64 = 1e18 as i64;

fn main() {
    input! {
        n: usize, l: i64,
        x: [i64; n],
        t: [i64; n]
    };

    assert!(n <= 3000);

    let lp = l*2;

    // 0: left-left
    // 1: left-right (right-left)
    // 2: right-right
    let mut costs = vec![vec![0; 3]; n];
    for i in 0..n {
        // -> [*]
        let time_l = x[i]*2;
        // [*] <-
        let time_r = lp-x[i]*2;

        // l-r
        costs[i][1] = (t[i] + lp - 1) / lp * lp;

        // l-l
        costs[i][0] = time_r + (max(0, t[i]-time_r)+lp-1)/lp*lp;

        // r-r
        costs[i][2] = time_l + (max(0, t[i]-time_l)+lp-1)/lp*lp;
    }


    let mut dp = vec![vec![INF; n+1]; n+1];
    dp[0][0] = 0;

    let mut ans = INF;
    for i in 0..n {
        let d = x[i] - if i >= 1 { x[i-1] } else { 0 };
        for w in 0..n+1 {
            let wi = w as i64;
            let base = dp[i][w];
            if base == INF {
                continue;
            }
            let to = base + (wi+1) * d * 2;
            if i < n-1 {
                if w >= 1 {
                    dp[i+1][w-1] = min(dp[i+1][w-1], to + costs[i][0]);
                }
                dp[i+1][w] = min(dp[i+1][w], to + costs[i][1]);
                dp[i+1][w+1] = min(dp[i+1][w+1], to + costs[i][2]);
            } else {
                let nd = (l-x[i]) * 2;
                // close
                ans = min(ans, to + costs[i][0] + wi * nd);

                // pass
                ans = min(ans, to + costs[i][1] + (wi + 1) * nd);

                // open
                ans = min(ans, to + costs[i][2] + (wi + 2) * nd);
            }
        }
    }

    println!("{}", ans);
}