<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>

#include <vector>
#include <cstring>
#include <functional>

using namespace std;

class Solution {
public:
    double largestSumOfAverages(vector<int>& nums, int k) {
        int n = nums.size();
        vector<int> prefix_sum(n + 1, 0); // Create a vector to store the prefix sums
        vector<vector<double>> memo(n, vector<double>(k + 1, 0)); // Create a 2D vector for memoization

        // Calculate the prefix sums
        for (int i = 0; i < n; ++i) {
            prefix_sum[i + 1] = prefix_sum[i] + nums[i];
        }

        // Recursive lambda function for depth-first search
        function<double(int, int)> dfs = [&](int start, int partitions) -> double {
            if (start == n) return 0; // Base case: no more elements to partition
            if (partitions == 1) // Base case: only one partition is left
                return static_cast<double>(prefix_sum[n] - prefix_sum[start]) / (n - start);

            if (memo[start][partitions] > 0) return memo[start][partitions]; // If value already computed return it

            double max_average = 0;
            for (int end = start; end < n; ++end) {
                double current_average = static_cast<double>(prefix_sum[end + 1] - prefix_sum[start]) / (end - start + 1);
                double rest_average = dfs(end + 1, partitions - 1);
                max_average = max(max_average, current_average + rest_average); // Update max_average if the sum of averages is larger
            }

            return memo[start][partitions] = max_average; // Memoize and return the result
        };

        return dfs(0, k); // Initiate the recursive search with the entire array and k partitions
    }
};
11000