<p>You are given <code>m</code> <code>arrays</code>, where each array is sorted in <strong>ascending order</strong>.</p>

<p>You can pick up two integers from two different arrays (each array picks one) and calculate the distance. We define the distance between two integers <code>a</code> and <code>b</code> to be their absolute difference <code>|a - b|</code>.</p>

<p>Return <em>the maximum distance</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> arrays = [[1,2,3],[4,5],[1,2,3]]
<strong>Output:</strong> 4
<strong>Explanation:</strong> One way to reach the maximum distance 4 is to pick 1 in the first or third array and pick 5 in the second array.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> arrays = [[1],[1]]
<strong>Output:</strong> 0
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>m == arrays.length</code></li>
	<li><code>2 &lt;= m &lt;= 10<sup>5</sup></code></li>
	<li><code>1 &lt;= arrays[i].length &lt;= 500</code></li>
	<li><code>-10<sup>4</sup> &lt;= arrays[i][j] &lt;= 10<sup>4</sup></code></li>
	<li><code>arrays[i]</code> is sorted in <strong>ascending order</strong>.</li>
	<li>There will be at most <code>10<sup>5</sup></code> integers in all the arrays.</li>
</ul>

class Solution:
    def maxDistance(self, arrays: List[List[int]]) -> int:
        new = []
        for i, array in enumerate(arrays):
            for j in range(len(array)):
                new.append([array[j], i])
        new = sorted(new, key=lambda x: x[0])
        L = 0
        r = len(new)-1
        memo = {}
        def dfs(L, r):
            if L > r:
                return 0
            if (L, r) in memo:
                return memo[(L, r)]
            maximum = 0
            one = 0
            two = 0
            if new[L][1] != new[r][1]:
                one = abs(new[L][0]- new[r][0])
            else:
                one = dfs(L+1, r)
                two = dfs(L, r-1)
            maximum = max(one, two)
            memo[(L, r)] = maximum
            return memo[(L, r)]
        return dfs(L, r)

            

            


            


            

            



        """
        min_default_dict = collections.defaultdict(list)
        max_default_dict = collections.defaultdict(list)
        for i, looper in enumerate(arrays):
            minimum = float("inf")
            maximum = float("-inf")
            for r in range(len(looper)):
                minimum = min(minimum, looper[r])
                maximum = max(maximum, looper[r])
            min_default_dict[i] = minimum
            max_default_dict[i] = maximum
        print(min_default_dict)
        print(max_default_dict)
        return 0
        """

        

43659