# 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: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:

## CPP

`// 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 elements 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;` `}` |

## Python3

`# Python3 program for` `# the above approach` `N ` `=` `1005` `;` `K ` `=` `20` `;` `n ` `=` `0` `k ` `=` `0` `dp ` `=` `[[[` `-` `1` `for` `k ` `in` `range` `(K ` `+` `1` `)] ` `for` `j ` `in` `range` `(N ` `+` `1` `)] ` `for` `i ` `in` `range` `(N ` `+` `1` `)]` ` ` `# Function to calculate` `# min cost to choose` `# k increasing elements` `def` `minCost(i, prev, cnt, ele, cost):` ` ` `# If k elements 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` `100000` `;` ` ` ` ` `# To check if this is` ` ` `# already calculated` ` ` `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` `if` `__name__` `=` `=` `'__main__'` `:` ` ` ` ` `n ` `=` `4` `;` ` ` `k ` `=` `2` `;` ` ` ` ` `ele ` `=` `[ ` `0` `, ` `2` `, ` `6` `, ` `4` `, ` `8` `]` ` ` ` ` `cost ` `=` `[ ` `0` `, ` `40` `, ` `20` `, ` `30` `, ` `10` `]` ` ` ` ` `ans ` `=` `minCost(` `1` `, ` `0` `, ` `1` `, ele, cost);` ` ` ` ` `if` `(ans ` `=` `=` `100000` `):` ` ` `ans ` `=` `-` `1` `;` ` ` ` ` `print` `(ans)` ` ` `# This code is contributed by rutvik_56` |

**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. To complete your preparation from learning a language to DS Algo and many more, please refer **Complete Interview Preparation Course****.**

In case you wish to attend **live classes **with experts, please refer **DSA Live Classes for Working Professionals **and **Competitive Programming Live for Students**.