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++14 (Clang 3.8.0) (judge verdict: Wrong Answer)

#include <algorithm>
#include <bitset>
#include <cassert>
#include <chrono>
#include <climits>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <iomanip>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <cstdint>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i,n) for(int i = 0; i < (int)(n); ++i)
#define all(x) (x).begin(),(x).end()

template<typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val){
    std::fill( (T*)array, (T*)(array+N), val );
}
 
template<class T> inline bool chmax(T &a, T b){
    if(a<b){
        a = b;
        return true;
    }
    return false;
}

template<class T> inline bool chmin(T &a, T b){
    if(a>b){
        a = b;
        return true;
    }
    return false;
}

int main(){
    ll n;
    ll L;
    cin >> n >> L;
    vector<ll>x(n),t(n);
    rep(i,n)cin >> x[i];
    rep(i,n)cin >> t[i];
    ll res = 0;
    vector<int> s;
    rep(i,n){
        ll a = x[i];
        ll aa = a + t[i];
        ll ma = aa % (2*L);
        bool fa = 0;
        if(ma<=x[i]){
            aa = (aa/(2*L))*2*L + x[i];
        }else if(ma<=2*L-x[i]){
            fa = 1;
            aa = (aa/(2*L))*2*L + 2*L-x[i];    
        }else{
            aa = (aa/(2*L) + 1)*2*L + x[i];
        }
        ll b = 2*L-x[i];
        ll bb = b + t[i];
        ll mb = bb % (2*L);
        bool fb = 0;
        if(mb<=x[i]){
            bb = (bb/(2*L))*2*L + x[i];
        }else if(mb<=2*L-x[i]){
            fb = 1;
            bb = (bb/(2*L))*2*L + 2*L-x[i];    
        }else{
            bb = (bb/(2*L) + 1)*2*L + x[i];
        }
        if((!fa)&&fb){
            res += aa - a; 
        }else if((fa)&&(!fb)){
            res += (aa-a) -(2*L-2*x[i]);
            s.push_back(0);
        }else if(fa){
            res += (aa-a) -(2*L-2*x[i]);
            s.push_back(1);
        }else{
            res += (bb-b) -2*x[i];
            s.push_back(2);
        }
    }
    // cerr << res << endl;
    // for(auto x:s){
    //     cerr << x << " ";
    // }
    // cerr << endl;
    int m = s.size();
    if(m==0){
        cout << res + 2*L << endl;
        return 0;
    }
    ll ddd = m;
    rep(i,m){
        if(s[i]==2){
            ddd = i;
            break;
        }
    }
    ll c = 0;
    ll xx = 0;
    ll yy = 0;
    bool flag = 0;
    for(int i=0;i<ddd;i++){
        if(s[i]==0){
            xx++;
            flag = 1;
        }
        if(s[i]==1){
            if(xx>0){
                c++;
                xx--;
                flag = 0;
            }else{
                yy++;
                flag = 0;
            }
        }
    }
    ll xxx = 0;
    ll zz = 0;
    bool fflag = 0;
    for(int i=m-1;i>=ddd;i--){
        fflag = 1;
        if(s[i]==0)xxx++;
        if(s[i]==2){
            if(xxx>0){
                c++;
                xxx--;
            }else{
                zz++;
            }
        }
    }
    ll S = xx+xxx;
    c += S/2;
    S %= 2;
    cerr << c << " " << S << " " << yy << " " << zz << endl;
    if(zz!=0){
        c += yy + S + zz + 1;
    }else{
        if(yy==0||fflag||flag){
            c += yy + S + 1;
        }else{
            c += yy + S;
        }
    }
    cout << res + c*2*L << endl;
    return 0;
}