# Minimum cost to select K strictly increasing elements

Given an array and an integer K. Also given one more array which stores the cost of choosing elements from the first array. The task is to calculate the minimum cost of selecting K strictly increasing elements from the array.

Examples:

```Input: N = 4, K = 2
ele[] = {2, 6, 4, 8}
cost[] = {40, 20, 30, 10}
Output: 30
Explanation:
30 is the minimum cost by selecting elements
6 and 8 from the array with cost
10 + 20 respectively

Input: N = 11, K = 4
ele = {2, 6, 4, 8, 1, 3, 15, 9, 22, 16, 45}
cost = {40, 20, 30, 10, 50, 10, 20, 30, 40, 20, 10}
Output: 60
Explanation:
60 is the minimum cost by selecting elements
3, 15, 16, 45 from the array with cost
10 + 20 + 20 + 10 respectively
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

Approach:
The given problem can be easily solved using a dynamic programming approach. As the problem asks for increasing elements and then minimum cost then it is clear that we have to move by either selecting ith or not selecting ith element one by one and calculate the minimum cost for each.

Now, take a 3D DP array which stores our values of minimum cost, where cache[i][prev][cnt] stores the min-cost up to ith element, prev element and count of numbers considered till now.

There are 3 base conditions involved:

• If k elements are counted return 0.
• If all elements of array has been traversed return MAX_VALUE.
• Check if it’s already calculated in dp array.

Now comes the part of either selecting ith element or not selecting ith element:

• When ith elements is not considered ans = dp(i+1, prev, cnt, s, c)
• When the ith element is greater than previous element, check if adding its cost makes total cost minimum ans = min(ans, c[i] + dp(i+1, i, cnt+1, s, c))

Below is the implementation of the above approach:

 `// C++ program for ` `// the above approach ` `#include ` `using` `namespace` `std; ` ` `  `const` `int` `N = 1005; ` `const` `int` `K = 20; ` `int` `n, k; ` `int` `dp[N + 1][N + 1][K + 1]; ` ` `  `// Function to calculate ` `// min cost to choose ` `// k increasing elements ` `int` `minCost(``int` `i, ``int` `prev, ``int` `cnt,  ` `                    ``int` `ele[], ``int` `cost[]) ` `{ ` `    ``// If k elemnets are ` `    ``// counted return 0 ` `    ``if` `(cnt == k + 1) { ` `        ``return` `0; ` `    ``} ` ` `  `    ``// If all elements ` `    ``// of array has been ` `    ``// traversed then ` `    ``// return MAX_VALUE ` `    ``if` `(i == n + 1) { ` `        ``return` `1e5; ` `    ``} ` ` `  `    ``// To check if this is ` `    ``// already calculated ` `    ``int``& ans = dp[i][prev][cnt]; ` `    ``if` `(ans != -1) { ` `        ``return` `ans; ` `    ``} ` ` `  `    ``// When i'th elements ` `    ``// is not considered ` `    ``ans = minCost(i + 1, prev, cnt, ele, cost); ` ` `  `    ``// When the ith element ` `    ``// is greater than previous ` `    ``// element check if adding ` `    ``// its cost makes total cost minimum ` `    ``if` `(ele[i] > ele[prev]) { ` `        ``ans = min(ans, cost[i] + minCost(i + 1,  ` `                           ``i, cnt + 1, ele, cost)); ` `    ``} ` `    ``return` `ans; ` `} ` ` `  `// Driver code ` `int` `main() ` `{ ` ` `  `    ``memset``(dp, -1, ``sizeof``(dp)); ` `    ``n = 4; ` `    ``k = 2; ` ` `  `    ``int` `ele[n + 1] = { 0, 2, 6, 4, 8 }; ` ` `  `    ``int` `cost[n + 1] = { 0, 40, 20, 30, 10 }; ` ` `  `    ``int` `ans = minCost(1, 0, 1, ele, cost); ` ` `  `    ``if` `(ans == 1e5) { ` `        ``ans = -1; ` `    ``} ` ` `  `    ``cout << ans << endl; ` ` `  `    ``return` `0; ` `} `

Output:

```30
```

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

Be the First to upvote.

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.