VOOZH about

URL: https://www.geeksforgeeks.org/dsa/minimum-sum-possible-of-any-bracket-sequence-of-length-n/

⇱ Minimum sum possible of any bracket sequence of length N - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Minimum sum possible of any bracket sequence of length N

Last Updated : 11 Jul, 2025

Given a number N representing the length of a bracket sequence consisting of brackets '(', ')'. The actual sequence is not known beforehand. Given the values of both brackets '(' and ')' if placed at index in the expression. 
The task is to find the minimum sum possible of any bracket sequence of length N using the above information.
Here adj[i][0] represents the value assigned to ')' bracket at ith index and adj[i][1] represents the value assigned to '(' bracket at ith index. 
Constraints
 

  • There should be N/2 pairs made of brackets. That is, N/2 pairs of '(', ')'.
  • Find minimum sum of proper bracket expression.
  • Index starts from 0.


Examples
 

Input : N = 4 
 adj[N][2] ={{5000, 3000},
 {6000, 2000}, 
 {8000, 1000}, 
 {9000, 6000}} 
Output : 19000
Assigning first index as '(' for proper 
bracket expression is (_ _ _ . 
Now all the possible bracket expressions are ()() and (()). 
where '(' denotes as adj[i][1] and ')' denotes as adj[i][0]. 
Hence, for ()() sum is 3000+6000+1000+9000=19000.
and (()), sum is 3000+2000+8000+9000=220000. 
Thus answer is 19000

Input : N = 4 
 adj[N][2] = {{435, 111},
 {43, 33}, 
 {1241, 1111}, 
 {234, 22}}
Output : 1499


 


Algorithm
 

  1. The first element of the bracket sequence can only be '(', hence value of adj[0][1] is only of use at index 0.
  2. Call a function to find a proper bracket expression using dp as discussed in this article.
  3. Denote '(' as adj[i][1] and ')' as a adj[i][0].
  4. Find the minimum sum of all possible correct bracket expressions.
  5. Return the answer + adj[0][1].


Below is the implementation of the above approach: 
 


Output: 
19000

 

Time Complexity: O(N2)
 

Comment