CHEFBR - Editorial





Author: Dmytro Berezin
Tester 1: Minako Kojima
Tester 2: Shiplu Hawlader
Editorialist: Pawel Kacprzak






You are given a sequence S of length at most 100 consisting of integers which represents brackets. A negative integer -x is an opening bracket of type x while x is a closing bracket of type x.

A balanced sequence of brackets is defined as usual (see the full problem statement for clarity).

We call a subsequence s of S valid if and only if s is a balanced sequence of brackets. Your task is to count the number of valid subsequences of S modulo 10^9 + 7.


Define a dp*[j] table where dp*[j] := number of valid subsequence of S[i…j]. Notice that you can compute dp*[j] if you know dp[i + 1][j] and positions of a closing bracket of type x in range from i + 1 to j if S* is an opening bracket of type x.


Since input numbers can be quite large (up to 10^9) we want to compress them at the beginning in order to use them as array indices or use a hash table with O(1) lookup and insert time to do this.

Let pos*[j][x] := list of positions of occurences of closing bracket of type x in S[i…j]

We can compute pos table in O(n^3) time using simple iteration over all 3 dimensions of it.

Let dp*[j] := number of valid subsequences of S[i…j]

For simplicity, let’s set dp*[j] := 1 for i > j.

Let’s assume that we have dp[i + 1][j] already computed. We want to extend it to dp*[j]. There are two cases:

  1. We don’t take a bracket at the i-th position, so dp*[j] += dp[i + 1][j]
  2. If the bracket at the i-th position is an opening bracket of type x, we can try to take it to our subsequence.

While the first case is clear, the second one requires an explanation. If there are k closing brackets of type x in S[i + 1…j] we have k different possibilities to extend shorter subsequences, i.e. dp*[j] += dp[i + 1][b - 1] * dp[b + 1][j] where b is a single position of a closing bracket of type x in S[i + 1…j]. Since we have pos table computed, we can get values for b using a quick lookup.

Time Complexity:

The time complexity is O(N^3) because computing pos and dp tables takes this much time and any other computation step doesn’t have greater complexity.


To be uploaded soon.


To be uploaded soon.


It seems that the test cases were changed and solutions were not rejudged. My submission got 90 points, missing task #0 and if I resubmit it, it gets 100 points.

I believe such test case changes should be warned by e-mail or don’t make it default but let us choose to get an e-mail for such cases.


if(a* < 0 && a[j] > 0 && a* + a[j] == 0) {
dp*[j] = dp*[j-1] + dp[i+1][j] + 1;
} else {
dp*[j] = dp*[j-1] + dp[i+1][j] - dp[i+1][j-1];
for(k = i + 1; k < j; k++) {
if(a[k] < 0 && a[j] > 0 && a[k] + a[j] == 0) {
dp*[j] = dp*[j] + (dp*[k-1] - dp[i+1][k-1]) * (dp[k][j] - dp[k][j-1]);

Using the same definition for dp*[j] mentioned above. I'm weak at dp, but solving this question boosted me a lot :D


Can someone please explain it, I thought it was something related to Catalan numbers. and please explain the sample testcase also.


A very nice editorial. Liked it as well as “power of dp” very much.


check this out ! this editorial was super simple !


Similar question . BTW can somebody tell how can I edit the editorial to add this link?


I do not understand why DP*[j] is directly obtained from DP[i+1][j] (excluding i th bracket). I haave severe instict telling me to take DP*[j-1] too i.e. the bracket was added on the last of the sequence i to j-1


-1 -2 9 2 -3 -4 3 4 8 8 1

there are to possibilities for |1| brackets - use it or not

-1 -2 9 2 -3 -4 3 4 8 8 1

(strong are used, but not very visible)

Very similar with |2| => 2 * 2

-1 -2 9 2 -3 -4 3 4 8 8 1

For 9 there is just one possibility - not to use it…

-1 -2 9 2 -3 -4 3 4 8 8 1

for |3| again use or do not use it - if you use it, there re no paired brackets, if you decide not to use it you can use |4| or not, so for the

-3 -4 3 4 8 8

there are 3 possibilities => 2 * 2 * 3 = 12. Clear?


Hopefully test cases are better explained above, rest you can refer here:


The editorial is marked as community wiki and thus you can edit it. In the right side (middle) of the the place where the tags are written in the end, there’s an option to edit it. See carefully… “Related problems : to be updated soon” is written


@damn_me Are you talking about “edited 15 Dec '14, 19:39” option?


Just above it, there’s a line written as “Feel free to edit it” with a link.