A car factory has two assembly lines, and also given an  2D array a[2][] of size N which represent the time taken by each station. Each station is dedicated to some sort of work like engine fitting, body fitting, painting, and so on. So, a car chassis must pass through each of the n stations in order before exiting the factory. The two parallel assemble line perform same task.
From any line one can switch another line diagonally. A 2D array T[2][] of size N is also given, which represent  exit time to switch line 1 to line 2 diagonally.
Also each assembly line takes an entry time e_{i} and exit time x_{i} which may be different for the two lines.
For more clarity, refer the following diagram
The task is to computing the minimum time it will take to build a car chassis.
One can minimize the total time taken by performing following steps:
	 A car chassis must pass through all stations from 1 to N in order(in any of the two assembly lines). i.e. it cannot jump from station i to station j if they are not at one move distance.
	The car chassis can move one station forward in the same line, or one station diagonally in the other line. It incurs an extra cost T_{i,j} to move to station j from line i. No cost is incurred for movement in same line.
	The time taken in station j on line i is a_{i, j}.
Example 1:
Input: N = 4
a[2][] = {{4, 5, 3, 2}, 
      {2, 10, 1, 4}}
T[2][] = {{0,7, 4, 5},
           {0,9, 2, 8}}
e[2] = {10,12}, x[2] = {18,7}
Output: 35
Explanation: 
According to the TC, this would 
be the following diagram
The bold line shows the path covered by the 
car chassis for given input values. So the minimum 
time taken by the car is 35.
Your Task:
You don't need to read input or print anything. Your task is to complete the function carAssembly() which takes two array of integers a,T of size n and array of e and x of size 2 as parameters and returns an integer denoting the answer.
Expected Time Complexity: O(N)
Expected Auxiliary Space: O(1)
Constraints:
1 ≤ N ≤ 10^{5}
1 ≤ a[2][], T[2][], e[], x[] ≤ 10^{6}

Source: geeksforgeeks | Difficulty: MEDIUM

Starter code: from typing import List





from typing import List





class Solution:

    def carAssembly(self, n : int, a : List[List[int]], T : List[List[int]], e : List[int], x : List[int]) -> int:

        # code here

        



Solutions:
from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		a[0][-1] += x[0]
		a[1][-1] += x[1]
		a[0][0] += e[0]
		a[1][0] += e[1]
		for i in range(n - 2, -1, -1):
			a[0][i] = a[0][i] + min(a[0][i + 1], T[0][i + 1] + a[1][i + 1])
			a[1][i] = a[1][i] + min(a[1][i + 1], T[1][i + 1] + a[0][i + 1])
		return min(a[0][0], a[1][0])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n, a, T, e, x):
		line0 = e[0] + a[0][0]
		line1 = e[1] + a[1][0]
		for i in range(1, len(a[0])):
			curLine0 = a[0][i] + min(line0, line1 + T[1][i])
			curLine1 = a[1][i] + min(line1, line0 + T[0][i])
			line0 = curLine0
			line1 = curLine1
		return min(line0 + x[0], line1 + x[1])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		n = len(a[0])
		prev1 = a[0][n - 1] + x[0]
		prev2 = a[1][n - 1] + x[1]
		for i in range(n - 2, -1, -1):
			curr1 = a[0][i] + min(prev1, T[0][i + 1] + prev2)
			curr2 = a[1][i] + min(prev2, T[1][i + 1] + prev1)
			(prev1, prev2) = (curr1, curr2)
		curr1 += e[0]
		curr2 += e[1]
		return min(curr1, curr2)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n, a, T, e, x):
		minOne = a[0][n - 1] + x[0]
		minTwo = a[1][n - 1] + x[1]
		for j in range(n - 2, 0, -1):
			one = a[0][j] + min(minOne, T[0][j + 1] + minTwo)
			two = a[1][j] + min(minTwo, T[1][j + 1] + minOne)
			(minOne, minTwo) = (one, two)
		result = min(e[0] + a[0][0] + min(minOne, T[0][1] + minTwo), e[1] + a[1][0] + min(minTwo, T[1][1] + minOne))
		return result


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:

		def ans(a, t, e, x, i, j, n):
			prev1 = e[0] + a[0][0]
			prev2 = e[1] + a[1][0]
			for i in range(1, n):
				curr1 = min(prev1 + a[0][i], prev2 + t[1][i] + a[0][i])
				curr2 = min(prev2 + a[1][i], prev1 + t[0][i] + a[1][i])
				prev1 = curr1
				prev2 = curr2
			return min(prev1 + x[0], prev2 + x[1])
		return ans(a, T, e, x, 0, 0, n)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:

		def ans(a, t, e, x, i, j, n):
			l1 = [-1] * n
			l2 = [-1] * n
			l1[0] = e[0] + a[0][0]
			l2[0] = e[1] + a[1][0]
			for i in range(1, n):
				l1[i] = min(l1[i - 1] + a[0][i], l2[i - 1] + t[1][i] + a[0][i])
				l2[i] = min(l2[i - 1] + a[1][i], l1[i - 1] + t[0][i] + a[1][i])
			return min(l1[n - 1] + x[0], l2[n - 1] + x[1])
		return ans(a, T, e, x, 0, 0, n)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		dp = [[0] * (n + 2) for _ in range(2)]
		dp[0][0] = e[0]
		dp[1][0] = e[1]
		nT = [T[0] + [10 ** 8], T[1] + [10 ** 8]]
		nT[0][0] = 10 ** 8
		nT[1][0] = 10 ** 8
		nA = [[e[0]] + a[0] + [x[0]], [e[1]] + a[1] + [x[1]]]
		for i in range(1, n + 2):
			dp[0][i] = min(dp[0][i - 1] + nA[0][i], dp[1][i - 1] + nA[0][i] + nT[1][i - 1])
			dp[1][i] = min(dp[1][i - 1] + nA[1][i], dp[0][i - 1] + nA[1][i] + nT[0][i - 1])
		return min(dp[0][-1], dp[1][-1])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:

		def finder(l, a, T):
			if l == 0:
				(prev_m, prev_n) = (0, 0)
			else:
				(prev_m, prev_n) = finder(l - 1, a, T)
			m = min(prev_m, prev_n + T[1][l])
			n = min(prev_n, prev_m + T[0][l])
			return (m + a[0][l], n + a[1][l])
		a[0][0] += e[0]
		a[1][0] += e[1]
		a[0][-1] += x[0]
		a[1][-1] += x[1]
		(prev_m, prev_n) = (0, 0)
		for l in range(n):
			m = min(prev_m, prev_n + T[1][l])
			n = min(prev_n, prev_m + T[0][l])
			(prev_m, prev_n) = (m + a[0][l], n + a[1][l])
		return min(prev_m, prev_n)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		a1 = e[0] + a[0][0]
		a2 = e[1] + a[1][0]
		for i in range(1, n):
			temp = a1
			a1 = a[0][i] + min(a1, a2 + T[1][i])
			a2 = a[1][i] + min(a2, temp + T[0][i])
		ans = min(a1 + x[0], a2 + x[1])
		return ans


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:

		def func(i, l, a, T, s, n, d):
			if i == n - 1:
				d[i][l] = s + a[l][i] + x[l]
				return d[i][l]
			if l == 0:
				a = min(func(i + 1, 0, a, T, s + a[0][i], n, d), func(i + 1, 1, a, T, s + a[0][i] + T[0][i + 1], n, d))
				if d[i][l] != None:
					d[i][l] = min(d[i][l], a)
				else:
					d[i][l] = a
				return d[i][l]
			else:
				a = min(func(i + 1, 1, a, T, s + a[1][i], n, d), func(i + 1, 0, a, T, s + a[1][i] + T[1][i + 1], n, d))
				if d[i][l] != None:
					d[i][l] = min(d[i][l], a)
				else:
					d[i][l] = a
				return d[i][l]
		a0 = x[0] + a[0][n - 1]
		a1 = x[1] + a[1][n - 1]
		for i in range(n - 2, -1, -1):
			x = min(a0 + a[0][i], a1 + a[0][i] + T[0][i + 1])
			y = min(a1 + a[1][i], a0 + a[1][i] + T[1][i + 1])
			(a0, a1) = (x, y)
		a0 += e[0]
		a1 += e[1]
		return min(a0, a1)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], t: List[List[int]], e: List[int], x: List[int]) -> int:
		time1 = [float('inf') for _ in range(n)]
		time2 = [float('inf') for _ in range(n)]
		time1[0] = e[0] + a[0][0]
		time2[0] = e[1] + a[1][0]
		for i in range(1, n):
			time1[i] = a[0][i] + min(time1[i - 1], time2[i - 1] + t[1][i])
			time2[i] = a[1][i] + min(time2[i - 1], time1[i - 1] + t[0][i])
		return min(time1[n - 1] + x[0], time2[n - 1] + x[1])


from typing import List
from typing import List
import sys
sys.setrecursionlimit(10 ** 6)

class Solution:

	def solve(self, line, n, arr, time, dp, e):
		if n == 0:
			return arr[line][0] + e[line]
		if dp[n][line] != -1:
			return dp[n][line]
		opt1 = arr[line][n] + self.solve(line, n - 1, arr, time, dp, e)
		opt2 = arr[line][n] + time[not line][n] + self.solve(not line, n - 1, arr, time, dp, e)
		dp[n][line] = min(opt1, opt2)
		return dp[n][line]

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		n = len(a[0])
		dp1 = [0] * (n + 1)
		dp2 = [0] * (n + 1)
		dp = [[-1 for i in range(2)] for j in range(n + 1)]
		dp[0][0] = a[0][0] + e[0]
		dp[0][1] = a[1][0] + e[1]
		for i in range(1, n):
			for j in range(2):
				dp[i][j] = min(dp[i - 1][j] + a[j][i], dp[i - 1][not j] + a[j][i] + T[not j][i])
		return min(dp[n - 1][0] + x[0], dp[n - 1][1] + x[1])


from typing import List
from typing import List

def carAssembleTime(a, t, e, x):
	n = len(a[0])
	first = e[0] + a[0][0]
	second = e[1] + a[1][0]
	for i in range(1, n):
		up = min(first + a[0][i], second + t[1][i] + a[0][i])
		down = min(second + a[1][i], first + t[0][i] + a[1][i])
		(first, second) = (up, down)
	first += x[0]
	second += x[1]
	return min(first, second)

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		return carAssembleTime(a, T, e, x)


from typing import List
from typing import List

def carAssembly(a, t, e, x):
	NUM_STATION = len(a[0])
	T1 = [0 for i in range(NUM_STATION)]
	T2 = [0 for i in range(NUM_STATION)]
	T1[0] = e[0] + a[0][0]
	T2[0] = e[1] + a[1][0]
	for i in range(1, NUM_STATION):
		T1[i] = min(T1[i - 1] + a[0][i], T2[i - 1] + t[1][i] + a[0][i])
		T2[i] = min(T2[i - 1] + a[1][i], T1[i - 1] + t[0][i] + a[1][i])
	return min(T1[NUM_STATION - 1] + x[0], T2[NUM_STATION - 1] + x[1])

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		return carAssembly(a, T, e, x)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], t: List[List[int]], e: List[int], x: List[int]) -> int:
		n = len(a[0])
		t1 = [0] * n
		t2 = [0] * n
		t1[0] = e[0] + a[0][0]
		t2[0] = e[1] + a[1][0]
		for i in range(1, n):
			t1[i] = min(t1[i - 1] + a[0][i], t2[i - 1] + t[1][i] + a[0][i])
			t2[i] = min(t2[i - 1] + a[1][i], t1[i - 1] + t[0][i] + a[1][i])
		return min(t1[-1] + x[0], t2[-1] + x[1])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		top = e[0] + a[0][0]
		low = e[1] + a[1][0]
		for i in range(1, len(a[0])):
			next_top = min(top, low + T[1][i]) + a[0][i]
			next_low = min(low, top + T[0][i]) + a[1][i]
			top = next_top
			low = next_low
		return min(top + x[0], low + x[1])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		dp = [[0 for i in range(n + 1)] for j in range(2)]
		dp[0][0] = e[0] + a[0][0]
		dp[1][0] = e[1] + a[1][0]
		for j in range(1, n + 1):
			for i in range(2):
				if j == n:
					dp[i][j] = dp[i][j - 1] + x[i]
				elif i == 0:
					dp[i][j] = min(dp[i][j - 1] + a[i][j], dp[i + 1][j - 1] + T[i + 1][j] + a[i][j])
				else:
					dp[i][j] = min(dp[i][j - 1] + a[i][j], dp[i - 1][j - 1] + T[i - 1][j] + a[i][j])
		return min(dp[0][n], dp[1][n])


from typing import List
from typing import List
import sys

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		exit = x
		n = len(T[0])
		dp = [[-1 for _ in range(n)] for _ in range(2)]
		dp[0][-1] = exit[0] + a[0][-1]
		dp[1][-1] = exit[1] + a[1][-1]
		for i in range(n - 1)[::-1]:
			dp[0][i] = min(dp[0][i + 1], dp[1][i + 1] + T[0][i + 1]) + a[0][i]
			dp[1][i] = min(dp[0][i + 1] + T[1][i + 1], dp[1][i + 1]) + a[1][i]
		return min(e[0] + dp[0][0], e[1] + dp[1][0])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n, a, t, e, x):
		pre1 = e[0] + a[0][0]
		pre2 = e[1] + a[1][0]
		for i in range(1, n):
			curr1 = a[0][i] + min(pre1, t[1][i] + pre2)
			curr2 = a[1][i] + min(pre2, t[0][i] + pre1)
			(pre1, pre2) = (curr1, curr2)
		pre1 += x[0]
		pre2 += x[1]
		return min(pre1, pre2)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n, a, t, e, x):
		dp = [[0 for j in range(n)] for i in range(2)]
		dp[0][0] = e[0] + a[0][0]
		dp[1][0] = e[1] + a[1][0]
		for i in range(1, n):
			dp[0][i] = a[0][i] + min(dp[0][i - 1], t[1][i] + dp[1][i - 1])
			dp[1][i] = a[1][i] + min(dp[1][i - 1], t[0][i] + dp[0][i - 1])
		dp[0][n - 1] += x[0]
		dp[1][n - 1] += x[1]
		return min(dp[0][n - 1], dp[1][n - 1])


from typing import List
from typing import List

class Solution:

	def solve(self, i, j, arr, t, dp):
		if j == 0:
			return arr[i][0]
		if dp[i][j] != -1:
			return dp[i][j]
		if i == 0:
			dp[i][j] = arr[0][j] + min(t[1][j] + self.solve(1, j - 1, arr, t, dp), self.solve(0, j - 1, arr, t, dp))
		else:
			dp[i][j] = arr[1][j] + min(t[0][j] + self.solve(0, j - 1, arr, t, dp), self.solve(1, j - 1, arr, t, dp))
		return dp[i][j]

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		a[0][0] = a[0][0] + e[0]
		a[1][0] = a[1][0] + e[1]
		for j in range(1, n):
			a[0][j] = a[0][j] + min(a[0][j - 1], a[1][j - 1] + T[1][j])
			a[1][j] = a[1][j] + min(a[1][j - 1], a[0][j - 1] + T[0][j])
		a[0][n - 1] = a[0][n - 1] + x[0]
		a[1][n - 1] = a[1][n - 1] + x[1]
		return min(a[0][n - 1], a[1][n - 1])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		f = e[0] + a[0][0]
		b = e[1] + a[1][0]
		for i in range(1, n):
			c = min(f + a[0][i], b + a[0][i] + T[1][i])
			d = min(b + a[1][i], f + a[1][i] + T[0][i])
			f = c
			b = d
		return min(f + x[0], b + x[1])


class Solution:

	def carAssembly(self, n, a, T, e, x):
		dp = [[-1 for i in range(2)] for j in range(n)]
		dp[0][0] = e[0] + a[0][0]
		dp[0][1] = e[1] + a[1][0]
		for i in range(1, n):
			for j in range(2):
				first = a[j][i] + dp[i - 1][j]
				if j == 0:
					second = a[j][i] + T[j + 1][i] + dp[i - 1][j + 1]
				else:
					second = a[j][i] + T[j - 1][i] + dp[i - 1][j - 1]
				dp[i][j] = min(first, second)
		first = dp[n - 1][0] + x[0]
		second = dp[n - 1][1] + x[1]
		return min(first, second)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		ns = len(a[0])
		T1 = [0 for i in range(ns + 1)]
		T2 = [0 for i in range(ns + 1)]
		T1[0] = e[0] + a[0][0]
		T2[0] = e[1] + a[1][0]
		for i in range(1, ns):
			T1[i] = min(T1[i - 1] + a[0][i], T2[i - 1] + T[1][i] + a[0][i])
			T2[i] = min(T2[i - 1] + a[1][i], T1[i - 1] + T[0][i] + a[1][i])
		return min(T1[ns - 1] + x[0], T2[ns - 1] + x[1])

class IntMatrix:

	def __init__(self) -> None:
		pass

	def Input(self, n, m):
		matrix = []
		for _ in range(n):
			matrix.append([int(i) for i in input().strip().split()])
		return matrix

	def Print(self, arr):
		for i in arr:
			for j in i:
				print(j, end=' ')
			print()

class IntArray:

	def __init__(self) -> None:
		pass

	def Input(self, n):
		arr = [int(i) for i in input().strip().split()]
		return arr

	def Print(self, arr):
		for i in arr:
			print(i, end=' ')
		print()
if __name__ == '__main__':
	t = int(input())
	for _ in range(t):
		n = int(input())
		a = IntMatrix().Input(2, 2)
		T = IntMatrix().Input(2, 2)
		e = IntArray().Input(2)
		x = IntArray().Input(2)
		obj = Solution()
		res = obj.carAssembly(n, a, T, e, x)
		print(res)


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		if n == 1:
			return min(a[0][0] + e[0] + x[0], a[1][0] + e[1] + x[1])
		a[0][0] += e[0]
		a[0][n - 1] += x[0]
		a[1][0] += e[1]
		a[1][n - 1] += x[1]
		prev1 = a[0][0]
		prev2 = a[1][0]
		for i in range(1, n):
			next1 = a[0][i] + min(prev1, T[1][i] + prev2)
			next2 = a[1][i] + min(prev2, T[0][i] + prev1)
			prev1 = next1
			prev2 = next2
		return min(prev1, prev2)


from typing import List
from typing import List
import numpy as np
import sys

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		dp = [[0 for i in range(n)] for j in range(2)]
		for i in range(2):
			a[i][0] += e[i]
			a[i][n - 1] += x[i]
		for i in range(2):
			dp[i][n - 1] = a[i][n - 1]
		for j in range(n - 2, -1, -1):
			for i in range(0, 2):
				up_diag = sys.maxsize
				if i == 1:
					up_diag = T[i][j + 1] + dp[0][j + 1]
				down_diag = sys.maxsize
				if i == 0:
					down_diag = T[i][j + 1] + dp[1][j + 1]
				right = 0 + dp[i][j + 1]
				dp[i][j] = a[i][j] + min(up_diag, min(down_diag, right))
		return min(dp[0][0], dp[1][0])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], t: List[List[int]], e: List[int], x: List[int]) -> int:
		dp1 = [0] * (n + 3)
		dp2 = [0] * (n + 3)
		dp1[0] = e[0] + a[0][0]
		dp2[0] = e[1] + a[1][0]
		for i in range(1, n):
			dp1[i] = min(dp1[i - 1] + a[0][i], dp2[i - 1] + t[1][i] + a[0][i])
			dp2[i] = min(dp2[i - 1] + a[1][i], dp1[i - 1] + t[0][i] + a[1][i])
		return min(dp1[n - 1] + x[0], dp2[n - 1] + x[1])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		n = len(a[0])
		dp = [[0 for i in range(n + 1)] for j in range(2)]
		dp[0][0] = e[0]
		dp[1][0] = e[1]
		dp[0][1] = e[0] + a[0][0]
		dp[1][1] = e[1] + a[1][0]
		for j in range(2, n + 1):
			ans1 = dp[0][j - 1] + a[0][j - 1]
			ans2 = dp[1][j - 1] + T[1][j - 1] + a[0][j - 1]
			dp[0][j] = min(ans2, ans1)
			ans4 = dp[1][j - 1] + a[1][j - 1]
			ans3 = dp[0][j - 1] + T[0][j - 1] + a[1][j - 1]
			dp[1][j] = min(ans3, ans4)
		fir = dp[0][n]
		sec = dp[1][n]
		return min(fir + x[0], sec + x[1])


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		first = e[0] + a[0][0]
		second = e[1] + a[1][0]
		for i in range(1, n):
			one = min(first + a[0][i], second + T[1][i] + a[0][i])
			two = min(second + a[1][i], first + T[0][i] + a[1][i])
			first = one
			second = two
		ans = min(first + x[0], second + x[1])
		return ans


from typing import List
from typing import List

class Solution:

	def carAssembly(self, n: int, a: List[List[int]], T: List[List[int]], e: List[int], x: List[int]) -> int:
		a[0][0] += e[0]
		a[1][0] += e[1]
		I = len(a[0])
		for i in range(1, I):
			a[0][i] += min(a[0][i - 1], a[1][i - 1] + T[1][i])
			a[1][i] += min(a[1][i - 1], a[0][i - 1] + T[0][i])
		a0 = a[0][-1]
		a1 = a[1][-1]
		a0 = a0 + x[0]
		a1 = a1 + x[1]
		return min(a0, a1)


from typing import List
from typing import List

def solve(arr, t, n, e, x):
	dp = [[0 for i in range(n)] for j in range(2)]
	dp[0][0] = arr[0][0] + e[0]
	dp[1][0] = arr[1][0] + e[1]
	for i in range(1, n):
		dp[0][i] = min(arr[0][i] + dp[0][i - 1], arr[0][i] + dp[1][i - 1] + t[1][i])
		dp[1][i] = min(arr[1][i] + dp[1][i - 1], arr[1][i] + dp[0][i - 1] + t[0][i])
	a = dp[0][n - 1] + x[0]
	b = dp[1][n - 1] + x[1]
	return min(a, b)

class Solution:

	def carAssembly(self, n: int, arr: List[List[int]], t: List[List[int]], e: List[int], x: List[int]) -> int:
		ans = solve(arr, t, n, e, x)
		return ans