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 (GCC 5.4.1) (judge verdict: Wrong Answer)

#include <stdio.h>
#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <deque>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#pragma warning(disable:4996)
#pragma comment(linker, "/STACK:336777216")
using namespace std;

#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define ldb ldouble

typedef tuple<int, int, int> t3;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
typedef long double ldb;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef pair <ll, int> pli;
typedef pair <db, db> pdd;

int IT_MAX = 1 << 17;
int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const ll LL_INF = 0x3f3f3f3f3f3f3f3f;
const db PI = acos(-1);
const db ERR = 1e-10;
#define szz(x) (int)(x).size()
#define Se second
#define Fi first

ll X[3050];
ll T[3050];
pll mycalc(ll i, ll t, ll L) {
	pll rv;
	rv.first = t / (2 * L);
	
	ll tu = t % (2 * L);
	if (tu <= X[i]) rv.second = 0;
	else if (tu <= 2 * L - X[i]) rv.second = 1;
	else {
		rv.second = 0;
		rv.first++;
	}
	return rv;
}

vector <ll> Vin;
ll dp[3050][2][2];
ll tdp[3050][2][2];
int main() {
	ll N, L, i, j, k;
	scanf("%lld %lld", &N, &L);
	if (N > 3000) return !printf("Good Problem!\n");

	for (i = 1; i <= N; i++) scanf("%lld", &X[i]);
	for (i = 1; i <= N; i++) scanf("%lld", &T[i]);

	ll ans = 0;
	int lst = 0;
	for (i = 1; i <= N; i++) {
		pll v1 = mycalc(i, X[i] + T[i], L);
		pll v2 = mycalc(i, 2 * L - X[i] + T[i], L);

		if (v1.second == v2.second) {
			ll en = v1.second, st;
			if (v1.first == v2.first) st = 1;
			else st = 0;
			ans += v1.first;

			if (st != en) {
				Vin.push_back(st);
				lst = i;
			}
		}
		else if (v1.second == 0) {
			assert(v1.first == v2.first);
			ans += v1.first;
		}
		else {
			assert(v1.first + 1 == v2.first);
			ans += v1.first;
			Vin.push_back(2);

			lst = i;
		}
	}
	if (Vin.empty()) return !printf("%lld\n", (ans + 1) * 2 * L);

	memset(dp, 0x3f, sizeof(dp));
	dp[0][0][0] = 0;
	for (auto it : Vin) {
		int st = 0, en = 1;
		if (it == 0) en = 0;
		if (it == 1) st = 1;
		memset(tdp, 0x3f, sizeof(tdp));

		for (i = 0; i <= N; i++) {
			for (j = 0; j < 2; j++) {
				for (int tx = 0; tx < 2; tx++) {
					for (k = st; k <= en; k++) {
						ll nv = dp[i][j][tx];

						int ni = i, nj = j, ntx = tx;
						if (k == 1) {
							ni++;
							if (st != en) nv++;
							ntx = 1;
						}
						else {
							if (i == 0) nv++;
							else {
								if (nj == 0) nv++;
								ni--;
								nj = 1;
							}
						}
						tdp[ni][nj][tx] = min(tdp[ni][nj][ntx], nv);
					}
				}
			}
		}
			for (i = 0; i <= N; i++) for (j = 0; j < 2; j++) for (int tx = 0; tx < 2; tx++) dp[i][j][tx] = tdp[i][j][tx];
	}

	ll mn = LL_INF;
	for (i = 0; i <= N; i++) {
		for (j = 0; j < 2; j++) {
			for (int tx = 0; tx < 2; tx++) {
				ll v = dp[i][j][tx] + i;
				if (tx == 0 && lst != N) v++;
				mn = min(mn, v);
			}
		}
	}
	return !printf("%lld\n", (ans+mn) * 2 * L);
}