Note: This is an approximate problem. There is no exact solution. You must find the most optimal solution. Please, take a look at the sample explanation to ensure that you understand the problem correctly.
Given a set of N nodes, where each node has a character A[i] associated with it. A[i] can be either an opening bracket ( or a closing bracket ). Construct a tree using N nodes numbered 1 to N such that the value of the given function Z is maximized.
Z=i=N∑i=1[j=N∑j=i+1[F(i,j)]], where F(i,j) = Length of the longest balanced parenthesis substring on the simple path between node i and node j.
A string is said to be balanced if
For example : (())) is not balanced as last closing bracket is unmatched.
Input format
Output format
Constraints
2≤N≤500
Verdict and Scoring
Value of F(i,j) are as follows:
Hence, Z = 2 + 2 = 4.