<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 {
public:
    double largestSumOfAverages(vector<int>& nums, int k) {
        
        int n = nums.size();
        vector<vector<vector<double>>> dp(n);

        vector<int> prefix(n);

        prefix[0] = nums[0];
        for(int i=1;i<n;i++){
            prefix[i] = prefix[i-1] + nums[i];
        }
   
        //matrice n*n*K
        for(int i=0;i<n;i++){
            dp[i].resize(n);
        }

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                dp[i][j].resize(k);
            }
        }

        //caso base
        for(int j=0;j<n;j++){
            for(int i=0;i<n;i++){

                if(j >= i){
                    if(j == i){
                        dp[i][j][0] = nums[i];
                    }else{
                    int leftSum = (i==0) ? 0 : prefix[i-1];
                    int rightSum = prefix[n-1] - prefix[j];
                    dp[i][j][0] = ((double) (prefix[n-1] - leftSum  - rightSum)) / (j-i+1);
                    }
                }
            }
        }

        //passo induttivo
        for(int K=1;K<k;K++){

            for(int j=0;j<n;j++){

                for(int i=0;i<n;i++){
                
                    if(j >= i){

                        double maximum;

                        int z = i;

                        maximum = dp[i][j][0];
                        while(z < j){

                            double sum_avg1,sum_avg2;
                            double avg1,avg2;

                            int n_part1,n_part2;
                            if((z-i) > (K-1)){
                                avg1 = dp[i][z][K-1];
                                n_part1 = K-1;
                            }else{
                                avg1 = dp[i][z][z-i];
                                n_part1 = z-i;
                            }

                            if((j-z-1) > (K-1)){
                                avg2 = dp[z+1][j][K-1];
                                n_part2 = K-1;
                            }else{
                                avg2 = dp[z+1][j][j-z-1];
                                n_part2 = j-z-1;
                            }
                     
                            sum_avg1 = avg1 + dp[z+1][j][abs(K-1-n_part1)];
                            sum_avg2 = avg2 + dp[i][z][abs(K-1-n_part2)];
                
                            maximum = max(max(sum_avg1,sum_avg2),maximum);

                            z = z + 1;
                        }
            
                        dp[i][j][K] = maximum;
                    }
                }
            }
        }
        return dp[0][n-1][k-1];
    }
};
43100