<p>Given an integer array <code>nums</code> and an integer <code>k</code>, split <code>nums</code> into <code>k</code> non-empty subarrays such that the largest sum of any subarray is <strong>minimized</strong>.</p>

<p>Return <em>the minimized largest sum of the split</em>.</p>

<p>A <strong>subarray</strong> is a contiguous part of the array.</p>

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

<pre>
<strong>Input:</strong> nums = [7,2,5,10,8], k = 2
<strong>Output:</strong> 18
<strong>Explanation:</strong> There are four ways to split nums into two subarrays.
The best way is to split it into [7,2,5] and [10,8], where the largest sum among the two subarrays is only 18.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5], k = 2
<strong>Output:</strong> 9
<strong>Explanation:</strong> There are four ways to split nums into two subarrays.
The best way is to split it into [1,2,3] and [4,5], where the largest sum among the two subarrays is only 9.
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 1000</code></li>
	<li><code>0 &lt;= nums[i] &lt;= 10<sup>6</sup></code></li>
	<li><code>1 &lt;= k &lt;= min(50, nums.length)</code></li>
</ul>

class Solution:
    def splitArray(self, nums: List[int], k: int) -> int:
        cum_sum = [0]
        for n in nums:
            cum_sum.append(cum_sum[-1] + n)
        max_from_start = [nums[-1]]
        for i in range(len(nums)):
            max_from_start.append(max(max_from_start[-1], nums[len(nums) - i - 2]))
        max_from_start = max_from_start[::-1]
        
        mem = {}
        def largest_subarray_sum(start, nsplits):
            if len(nums) - start == nsplits:
                return max_from_start[start]#max(nums[start:])
            if nsplits == 1:
                return cum_sum[-1] - cum_sum[start] #sum(nums[start:])
            if (start, nsplits) in mem:
                return mem[(start, nsplits)]
            res = cum_sum[-1]
            for new_start in range(start + 1, len(nums) - (nsplits - 1) + 1):
                if res < cum_sum[new_start] - cum_sum[start]:
                    break
                cur_res = max(cum_sum[new_start] - cum_sum[start], largest_subarray_sum(new_start, nsplits - 1))
                res = min(res, cur_res)
            mem[(start, nsplits)] = res
            return res
        
        return largest_subarray_sum(0, k)
22300