Minimum sum possible of any bracket sequence of length N | C ++ program to find the Minimum sum possible of any bracket sequence of length N using the given values for brackets ; DP array ; Recursive function to check for correct bracket expression ; / Not a proper bracket expression ; If reaches at end ; / If proper bracket expression ; else if not , return max ; If already visited ; To find out minimum sum ; Driver Code
#include <bits/stdc++.h> NEW_LINE using namespace std ; #define MAX_VAL  10000000 NEW_LINE int dp [ 100 ] [ 100 ] ; int find ( int index , int openbrk , int n , int adj [ ] [ 2 ] ) { if ( openbrk < 0 ) return MAX_VAL ; if ( index == n ) { if ( openbrk == 0 ) { return 0 ; } return MAX_VAL ; } if ( dp [ index ] [ openbrk ] != -1 ) return dp [ index ] [ openbrk ] ; dp [ index ] [ openbrk ] = 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 ] ; } int main ( ) { int n = 4 ; int adj [ n ] [ 2 ] = { { 5000 , 3000 } , { 6000 , 2000 } , { 8000 , 1000 } , { 9000 , 6000 } } ; memset ( dp , -1 , sizeof ( dp ) ) ; cout << find ( 1 , 1 , n , adj ) + adj [ 0 ] [ 1 ] << endl ; return 0 ; }