// C# program to find the Minimum sum possible
// of any bracket sequence of length N using
// the given values for brackets
using System;
class GFG
{
public static int MAX_VAL = 10000000;
// DP array
public static int[,] dp = new int[100,100];
// Recursive function to check for
// correct bracket expression
public static int find(int index, int openbrk, int n, int[,] adj)
{
/// Not a proper bracket expression
if (openbrk < 0)
return MAX_VAL;
// If reaches at end
if (index == n) {
/// If proper bracket expression
if (openbrk == 0) {
return 0;
}
else // if not, return max
return MAX_VAL;
}
// If already visited
if (dp[index,openbrk] != -1)
return dp[index,openbrk];
// To find out minimum sum
dp[index,openbrk] = Math.Min(adj[index,1] + find(index + 1,
openbrk + 1, n, adj),
adj[index,0] + find(index + 1,
openbrk - 1, n, adj));
return dp[index,openbrk];
}
// Driver Code
static void Main()
{
int n = 4;
int[,] adj = new int[,]{
{ 5000, 3000 },
{ 6000, 2000 },
{ 8000, 1000 },
{ 9000, 6000 }
};
for(int i = 0; i < 100; i++)
for(int j = 0; j < 100; j++)
dp[i,j] = -1;
Console.Write(find(1, 1, n, adj) + adj[0,1] + "\n");
}
//This code is contributed by DrRoot_
}