using System;
class GFG {
// Function to calculate
// min cost to choose
// k increasing elements
public static int minCost(int i, int prev, int cnt,
int[] ele, int[] cost, int n,
int k, int[, , ] dp)
{
// 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 1000000;
}
// 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, n, k,
dp);
// When the ith element
// is greater than previous
// element check if adding
// its cost makes total cost minimum
if (ele[i] > ele[prev]) {
ans = Math.Min(
ans, (cost[i]
+ minCost(i + 1, i, cnt + 1, ele,
cost, n, k, dp)));
}
return ans;
}
public static void Main(string[] args)
{
int N = 1005;
int K = 20;
int n = 4, k = 2;
int[, , ] dp = new int[N + 1, N + 1, K + 1];
// setting all values in dp = -1
for (int i = 0; i <= N; i++)
for (int j = 0; j <= N; j++)
for (int l = 0; l <= K; l++)
dp[i, j, l] = -1;
int[] ele = { 0, 2, 6, 4, 8 };
int[] cost = { 0, 40, 20, 30, 10 };
int ans = minCost(1, 0, 1, ele, cost, n, k, dp);
if (ans == 1000000) {
ans = -1;
}
Console.WriteLine(ans);
// This code is contributed by Aditya Sharma
}
}