ROCK - Sweet and Sour Rock

A manufacturer of sweets has started production of a new type of sweet called rock. Rock comes in sticks composed of one-centimetre-long segments, some of which are sweet, and the rest are sour. Before sale, the rock is broken up into smaller pieces by splitting it at the connections of some segments.

Today's children are very particular about what they eat, and they will only buy a piece of rock if it contains more sweet segments than sour ones. Try to determine the total length of rock which can be sold after breaking up the rock in the best possible way.


The input begins with the integer t, the number of test cases. Then t test cases follow.

For each test case, the first line of input contains one integer N - the length of the stick in centimetres (1<=N<=200). The next line is a sequence of N characters '0' or '1', describing the segments of the stick from the left end to the right end ('0' denotes a sour segment, '1' - a sweet one).


For each test case output a line with a single integer: the total length of rock that can be sold after breaking up the rock in the best possible way.


Sample input:

Sample output:

hide comments
Daniel Polaczanski: 2015-01-07 22:47:34

Do I good understand that for 0101001 the answer is 4?

Rishav Jain: 2014-12-23 08:55:43

any1 can plz explain how the answer of 1 st case is 9,?

Anuva Agarwal: 2014-10-04 15:13:13

phew! :)

AlcatraZ: 2014-10-02 14:04:38

Nice one..

Aravindan Chandrasekaran: 2014-09-17 14:13:40

Input string has more than 200 characters friends .

selfcompiler: 2014-08-11 20:59:27

simple Dp problem :)

Bartosz Bednarczyk: 2014-06-25 15:35:56

You're wrong. Recursion accepted.

nitesh kumar: 2014-06-24 08:08:50

O(n^3) solution..

|RAMSDEN|: 2014-06-19 10:09:45

Recursion gives tle
Iterative accepted:)

Harshit: 2014-06-05 06:36:53

you need to lessen the time limit

Added by:adrian
Time limit:7s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Resource:based on a problem from the VII Polish Collegiate Team Programming Contest (AMPPZ), 2002