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 Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

// package agc.agc022;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;

public class Main {
    private static final long INF = 1L<<60;

    public static void main(String[] args) {
        InputReader in = new InputReader(System.in);
        PrintWriter out = new PrintWriter(System.out);

        int n = in.nextInt();
        long L = in.nextInt();
        if (n > 3000) {
            out.println("");
            return;
        }
        long[] x = new long[n];
        for (int i = 0; i < n ; i++) {
            x[i] = in.nextInt();
        }
        long[] t = new long[n];
        for (int i = 0; i < n ; i++) {
            t[i] = in.nextInt();
        }

        long[][][] next = new long[2][2][n];
        for (int si = 0; si < n ; si++) {
            long X = x[si];
            for (int i = 0; i < 2; i++) {
                // i=0: from left, i=1: from right
                long leftTime = X;
                long rightTime = L * 2 - X;
                long fromTime = ((i == 0) ? leftTime : rightTime);
                long shopFinish = fromTime + t[si];
                for (int j = 0; j < 2 ; j++) {
                    long toTime = j == 0 ? leftTime : rightTime;
                    while (toTime < shopFinish) {
                        toTime += 2 * L;
                    }
                    next[i][j][si] = toTime - shopFinish;
                }
            }
        }

        long[][][][] dp = new long[2][2][n][n];
        for (int ni = 0; ni < 2 ; ni++) {
            for (int i = 0; i < 2; i++) {
                for (int j = 0; j < n; j++) {
                    Arrays.fill(dp[ni][i][j], INF);
                }
            }
        }
        for (int di = 0; di < 2 ; di++) {
            for (int si = 0; si < n ; si++) {
                long time = (di == 0) ? x[si] : L * 2 - x[si];
                dp[0][di][si][si] = dp[1][di][si][si] = time + t[si];
            }
        }
        
        long ans = INF;
        for (int len = 1 ; len < n ; len++) {
            for (int l = 0 ; l+len <= n ; l++) {
                int r = l+len-1;
                for (int di = 0 ; di <= 1 ; di++) {
                    for (int nw = 0 ; nw <= 1 ; nw++) {
                        long base = dp[nw][di][l][r];
                        if (base == INF) {
                            continue;
                        }
                        int now = nw == 1 ? r : l;
                        if (l >= 1) {
                            long ne = base + next[di][1][now] + (x[now] - x[l-1]) + t[l-1];
                            dp[0][1][l-1][r] = Math.min(dp[0][1][l-1][r], ne);
                            dp[0][0][l-1][r] = Math.min(dp[0][0][l-1][r], ne + x[l-1] * 2);
                        }
                        if (r + 1 < n) {
                            long ne = base + next[di][0][now] + (x[r+1] - x[now]) + t[r+1];
                            dp[1][0][l][r+1] = Math.min(dp[1][0][l][r+1], ne);
                            dp[1][1][l][r+1] = Math.min(dp[1][1][l][r+1], ne + (L - x[r+1]) * 2);
                        }
                    }
                }
            }
        }

        for (int nw = 0; nw <= 1; nw++) {
            int now = nw == 0 ? 0 : n-1;
            for (int di = 0 ; di <= 1 ; di++) {
                long base = dp[nw][di][0][n-1];
                long ne = base + next[di][1][now] + x[now];
                ans = Math.min(ans, ne);
            }
        }

        // debug(0, 0, 1, dp);


        out.println(ans);
        out.flush();
    }

    public static void debug(Object... o) {
        System.err.println(Arrays.deepToString(o));
    }

    public static class InputReader {
        private static final int BUFFER_LENGTH = 1 << 12;
        private InputStream stream;
        private byte[] buf = new byte[BUFFER_LENGTH];
        private int curChar;
        private int numChars;

        public InputReader(InputStream stream) {
            this.stream = stream;
        }

        private int next() {
            if (numChars == -1) {
                throw new InputMismatchException();
            }
            if (curChar >= numChars) {
                curChar = 0;
                try {
                    numChars = stream.read(buf);
                } catch (IOException e) {
                    throw new InputMismatchException();
                }
                if (numChars <= 0)
                    return -1;
            }
            return buf[curChar++];
        }

        public char nextChar() {
            return (char) skipWhileSpace();
        }

        public String nextToken() {
            int c = skipWhileSpace();
            StringBuilder res = new StringBuilder();
            do {
                res.append((char) c);
                c = next();
            } while (!isSpaceChar(c));
            return res.toString();
        }

        public int nextInt() {
            return (int) nextLong();
        }

        public long nextLong() {
            int c = skipWhileSpace();
            long sgn = 1;
            if (c == '-') {
                sgn = -1;
                c = next();
            }
            long res = 0;
            do {
                if (c < '0' || c > '9') {
                    throw new InputMismatchException();
                }
                res *= 10;
                res += c - '0';
                c = next();
            } while (!isSpaceChar(c));
            return res * sgn;
        }

        public double nextDouble() {
            return Double.valueOf(nextToken());
        }

        int skipWhileSpace() {
            int c = next();
            while (isSpaceChar(c)) {
                c = next();
            }
            return c;
        }

        boolean isSpaceChar(int c) {
            return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
        }
    }
}