this is a dp problem i have did this using recursion and memoization

but i am not able to think a iterative dp solution i mean top down approach

could any body help with explanation ?

## code

```
class Solution {
public:
const int inf = 1e9 + 7;
vector<vector<int>> dp;
int solve(vector<int>&arr,int i,int k){
if(i>=arr.size()){
return k==0?0:inf;
}
else if(k<=0) return inf;
else if(dp[i][k] != -1)
return dp[i][k];
int val = 0,ans = inf;
for(int j=i; j<arr.size()-k+1; j++){
val = max(val, arr[j]);
ans = min(ans, val + solve(arr,j+1,k-1));
}
return dp[i][k] = ans;
}
int minDifficulty(vector<int>& arr, int d) {
int n = arr.size();
dp.assign(n+1,vector<int>(d+1,-1));
int ans = solve(arr,0,d);
return ans==inf?-1:ans;
}
};
```