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 C# (Mono 4.6.2.0) (judge verdict: Wrong Answer)

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Text;
using System.Globalization;
using System.Diagnostics;



class Myon
{
    public Myon() { }
    public static int Main()
    {
        new Myon().calc();
        return 0;
    }


    Scanner cin;

    int C = 52;

    void calc()
    {
        cin = new Scanner();
        int N = cin.nextInt();
        long L = cin.nextInt();
        long[] x = cin.ArrayLong(N);
        long[] t = cin.ArrayLong(N);

        if (N > 100000) return;

        long ans = 0;
        for (int i = 0; i < N; i++)
        {
            long add = (t[i] - 1) / (2 * L);
            ans += add;
            t[i] -= 2 * L * add;
        }
        ans *= 2 * L;

        bool[] left = new bool[N];
        bool[] right = new bool[N];
        for (int i = 0; i < N; i++)
        {
            if (2 * x[i] >= t[i]) right[i] = true;
            if (2 * (L - x[i]) >= t[i]) left[i] = true;
        }

        if (!left[N - 1])
        {
            left[N - 1] = true;
            ans += 2 * L;
        }

        int N2 = N + 5;
        int MMIN = -9999;
        int[] dp = new int[N2];
        for (int i = 0; i < N2; i++)
        {
            dp[i] = MMIN;
        }
        dp[1] = 0;
        for (int i = 0; i < N; i++)
        {
            if (!left[i] && !right[i]) continue;
            int[] nextdp = new int[N2];
            for (int j = 0; j < N2; j++)
            {
                nextdp[j] = dp[j];
            }

            for (int j = 1; j <= N; j++)
            {
                if (dp[j] == MMIN) continue;
                if (left[i] && j != 0)
                {
                    nextdp[j - 1] = Math.Max(nextdp[j - 1], dp[j] + 1);
                }
                if (right[i])
                {
                    nextdp[j + 1] = Math.Max(nextdp[j + 1], dp[j]);
                }
            }
            dp = nextdp;
        }

        int maxfind = 0;
        for (int i = 0; i < N2; i++)
        {
            maxfind = Math.Max(dp[i], maxfind);
        }

        ans -= 2 * L * maxfind;
        //Console.WriteLine("maxfind : " + maxfind);

        /*
        for (int i = 0; i < N; i++)
        {
            Console.Write(left[i] ? "o" : "x");
            Console.Write(right[i] ? "o" : "x");
            Console.Write(" ");
        }
        Console.WriteLine();
        */

        ans += 2 * L * N;
        ans += 2 * L;
        Console.WriteLine(ans);

    }
}



class Scanner
{
    string[] s;
    int i;

    char[] cs = new char[] { ' ' };

    public Scanner()
    {
        s = new string[0];
        i = 0;
    }

    public string next()
    {
        if (i < s.Length) return s[i++];
        string st = Console.ReadLine();
        while (st == "") st = Console.ReadLine();
        s = st.Split(cs, StringSplitOptions.RemoveEmptyEntries);
        if (s.Length == 0) return next();
        i = 0;
        return s[i++];
    }

    public int nextInt()
    {
        return int.Parse(next());
    }
    public int[] ArrayInt(int N, int add = 0)
    {
        int[] Array = new int[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextInt() + add;
        }
        return Array;
    }

    public long nextLong()
    {
        return long.Parse(next());
    }

    public long[] ArrayLong(int N, long add = 0)
    {
        long[] Array = new long[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextLong() + add;
        }
        return Array;
    }

    public double nextDouble()
    {
        return double.Parse(next());
    }


    public double[] ArrayDouble(int N, double add = 0)
    {
        double[] Array = new double[N];
        for (int i = 0; i < N; i++)
        {
            Array[i] = nextDouble() + add;
        }
        return Array;
    }
}