<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:
    def largestSumOfAverages(self, nums: List[int], k: int) -> float:
        
        n = len(nums)

        if k == 1:
            return sum(nums) / n

        """
        def backtracking(n, nums, k, pos, ans):
            print(pos)
            if len(pos) == k:
                
                pos.append(n)
                avg = 0
                for i in range(len(pos) - 1):
                    start, end = pos[i], pos[i + 1]
                    avg += sum(nums[start:end]) / (end - start)

                ans[0] = max(ans[0], avg)
                pos.pop()
                return
            
            last = pos[-1]
            for i in range(last + 1, n):

                pos.append(i)
                backtracking(n, nums, k, pos, ans)
                pos.pop()

        ans = [0]
        pos = [0]
        backtracking(n, nums, k, pos, ans)

        return ans[0]
        """

        """
        def backtracking(n, nums, k, prev, pos, acc, ans):
            if pos + 1 == k:
                ans[0] = max(ans[0], acc + sum(nums[prev:n]) / (n - prev))
                return
            
            for i in range(prev + 1, n):
                backtracking(n, nums, k, i, pos + 1, acc + sum(nums[prev:i]) / (i - prev), ans)

        ans = [0]
        backtracking(n, nums, k, 0, 0, 0, ans)

        return ans[0]
        """

        


        # dp[i][j]: 前 i 個數字分成 j 個子數列的最高分
        dp = [[0 for _ in range(k + 1)] for _ in range(n + 1)]

        psum = [0]
        for i in range(1, n + 1):
            psum.append(psum[i - 1] + nums[i - 1])
        # print(psum)

        def average(i, j):
            return (psum[j] - psum[i]) / (j - i)

        for i in range(1, n + 1):
            dp[i][1] = average(0, i)
        # print(dp)

        for j in range(2, k + 1):
            for i in range(1, n + 1):
                for l in range(j, i + 1):
                    dp[i][j] = max(dp[i][j], dp[l - 1][j - 1] + average(l - 1, i))

        return dp[n][k]

16900