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:30Explanation:30 is the minimum cost by selecting elements 6 and 8 from the array with cost 10 + 20 respectivelyInput: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:60Explanation:60 is the minimum cost by selecting elements 3, 15, 16, 45 from the array with cost 10 + 20 + 20 + 10 respectively

**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 <bits/stdc++.h> ` `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; ` `} ` |

*chevron_right*

*filter_none*

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

## Recommended Posts:

- Minimum insertions to sort an array
- Minimum number of jumps to reach end
- Maximum sum such that no two elements are adjacent
- Longest Increasing Subsequence Size (N log N)
- Longest Increasing Subsequence | DP-3
- Min Cost Path | DP-6
- Maximum Sum Increasing Subsequence | DP-14
- Minimum insertions to form a palindrome | DP-28
- Remove minimum elements from either side such that 2*min becomes more than max
- Minimum Cost Polygon Triangulation
- Find the minimum cost to reach destination using a train
- Find minimum number of coins that make a given value
- Minimum steps to reach a destination
- Minimum number of squares whose sum equals to given number n
- Minimum Initial Points to Reach Destination
- Find minimum possible size of array with given rules for removing elements
- Partition a set into two subsets such that the difference of subset sums is minimum
- Minimum time to finish tasks without skipping two consecutive
- Longest Common Increasing Subsequence (LCS + LIS)
- Construction of Longest Increasing Subsequence(LIS) and printing LIS sequence

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.