<p>You are given an integer array <code>nums</code> and an integer <code>k</code>. You can partition the array into <strong>at most</strong> <code>k</code> non-empty adjacent subarrays. The <strong>score</strong> of a partition is the sum of the averages of each subarray.</p>

<p>Note that the partition must use every integer in <code>nums</code>, and that the score is not necessarily an integer.</p>

<p>Return <em>the maximum <strong>score</strong> you can achieve of all the possible partitions</em>. Answers within <code>10<sup>-6</sup></code> of the actual answer will be accepted.</p>

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

<pre>
<strong>Input:</strong> nums = [9,1,2,3,9], k = 3
<strong>Output:</strong> 20.00000
<strong>Explanation:</strong> 
The best choice is to partition nums into [9], [1, 2, 3], [9]. The answer is 9 + (1 + 2 + 3) / 3 + 9 = 20.
We could have also partitioned nums into [9, 1], [2], [3, 9], for example.
That partition would lead to a score of 5 + 2 + 6 = 13, which is worse.
</pre>

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

<pre>
<strong>Input:</strong> nums = [1,2,3,4,5,6,7], k = 4
<strong>Output:</strong> 20.50000
</pre>

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

<ul>
	<li><code>1 &lt;= nums.length &lt;= 100</code></li>
	<li><code>1 &lt;= nums[i] &lt;= 10<sup>4</sup></code></li>
	<li><code>1 &lt;= k &lt;= nums.length</code></li>
</ul>

class Solution:
    # Top-down DP memoization.
    # dp(i, k) = max(average(i, N), max_{j > i}(average(i, j) + dp(j, k-1)))

    def largestSumOfAverages(self, nums: List[int], K: int) -> float:
        n = len(nums) 
        presum = [0] * (n + 1) 
        for i in range(n): 
            presum[i + 1] = presum[i] + nums[i]
            
        dp = [[0] * (K + 1) for _ in range(n)]

        @cache
        def dfs(idx, k): # index in nums, remaining k 
            if k == 1: # we can't further partition, just take the avg
                # Calculate the average of [ nums[idx] , nums[n - 1] ]
                dp[idx][k] = (presum[n] - presum[idx]) / (n - idx)
                return dp[idx][k]
            # Try different partition dividing point at `i`
            for i in range(idx, n - (k - 1)):
                # avg from [ nums[idx] , nums[i] ]
                avg = (presum[i + 1] - presum[idx]) / (i - idx + 1)
                dp[idx][k] = max(dp[idx][k], avg + dfs(i + 1, k - 1))
            return dp[idx][k]

        return dfs(0, K)



    # def largestSumOfAverages(self, nums: List[int], K: int) -> float:
    #     n = len(nums)
    #     presum = []
    #     for i in range(len(nums)): 
    #         if i == 0:
    #             presum.append(nums[i])
    #         else:
    #             presum.append(presum[-1] + nums[i])
    #     dp = [[0] * (K+1) for _ in range(n)]

    #     @cache
    #     def dfs(idx, k): # index in nums, remaining k 
    #         if k == 1: # we can't further partition, just take the avg
    #             # DP for nums[idx : n]
    #             # we need to add back `nums[idx]`
    #             dp[idx][k] = (presum[n - 1] - presum[idx] + nums[idx]) / (n - idx)
    #             return dp[idx][k] 
    #         # try different partition dividing point at `i`
    #         i = idx
    #         while i + (k - 1) < n:
    #             avg = (presum[i] - presum[idx] + nums[idx]) / (i - idx + 1)
    #             dp[idx][k] = max(dp[idx][k], avg + dfs(i + 1, k - 1))
    #             i += 1
    #         return dp[idx][k]


    #     return dfs(0, K)

            

17800