<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:
    int pv[101];
    double dp[100][100][101];
    double f(int ind,int si,int rs,vector<int>& v){
        if(ind==v.size()){
            if(rs==0){
                if(si<v.size())
                    return INT_MIN;
                else
                    return 0;
            }
            else{
                int cs=pv[ind-1]-(si>0?pv[si-1]:0);
                int n=ind-1-si+1;

                double avg=(double)cs/n;

                return avg;
            }   
        }
        if(rs==0)
            return INT_MIN;
        if(dp[ind][si][rs]!=-1)
            return dp[ind][si][rs];

        double mxs,s1,s2,cs,n,avg;

        s1=f(ind+1,si,rs,v);
        cs=pv[ind]-(si>0?pv[si-1]:0);
        n=ind-si+1;

        avg=cs/n;

        s2=avg+f(ind+1,ind+1,rs-1,v);

        return dp[ind][si][rs]=max(s1,s2);
    }
    double largestSumOfAverages(vector<int>& v, int k) {
        memset(dp,-1.0,sizeof(dp));
        int n=v.size();
        int cs=0;
        for(int i=0;i<n;i++){
            cs+=v[i];
            pv[i]=cs;
        }

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                for(int x=0;x<=k;x++)
                    dp[i][j][x]=-1.0;
            }
        }
        return f(0,0,k,v);
    }
};
17800