// C# program to find Non-decreasing sequence
// of size k with minimum sum
using System;
class GFG {
public static int MAX = 100;
public static int inf = 1000000;
// Table used for memoization
public static int[, ] dp = new int[MAX, MAX];
// initialize
static void initialize()
{
for (int i = 0; i < MAX; i++)
for (int j = 0; j < MAX; j++)
dp[i, j] = -1;
}
// Function to find non-decreasing
// sequence of size k with minimum sum
static int solve(int[] arr, int i, int k)
{
int ans = 0;
// If already computed
if (dp[i, k] != -1)
return dp[i, k];
// Corner cases
if (i < 0)
return inf;
if (k == 1)
{
ans = inf;
for (int j = 0; j <= i; j++)
ans = Math.Min(ans, arr[i]);
return ans;
}
// Recursive computation
ans = inf;
for (int j = 0; j < i; j++)
if (arr[i] >= arr[j])
ans = Math.Min(ans, Math.Min(solve(arr, j, k),
solve(arr, j, k - 1) + arr[i]));
else
ans = Math.Min(ans, solve(arr, j, k));
dp[i, k] = ans;
return dp[i, k];
}
// driver program
public static void Main()
{
initialize();
int[] a = { 58, 12, 11, 12, 82, 30,
20, 77, 16, 86 };
int n = a.Length;
int k = 4;
Console.WriteLine(solve(a, n - 1, k));
}
}
// This code is contributed by vt_m