Getting WA on half the testcases of a recursive DP solution to INOI1602

This is the INOI16 problem 2: https://www.codechef.com/INOIPRAC/problems/INOI1602.

In this problem you are given a sequence of brackets, of length N: B[1], …, B[N], where each B[i] is one of the brackets. You are also given an array of Values: V[1],…, V[N].

Among all the subsequences in the Values array, such that the corresponding bracket subsequence in the B Array is a well-bracketed sequence, you need to find the maximum sum.

Note that closing bracket is opening bracket+K.

This was my code:

https://www.codechef.com/viewplaintext/21621266
It works for the first four tests, then for none after. Where is the problem?

The link shows “Access denied”. Can you explain your logic?