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
arbit: 2014-05-27 14:42:14

A test case I missed that can be helpful to you.


Ans 10

JordanBelfort: 2014-02-07 16:06:04

Really nice one..:)

Cloud Strife: 2014-02-06 06:31:21

Nice question!

harsh: 2013-11-29 14:01:11

nyc problem..

Dragan MarkoviƦ: 2013-07-21 18:59:28

I don't know if it's allowed but here's a little hint : When you have problem like this always think of regular expressions.

Prajit Patil: 2013-06-15 09:31:16

try this case

another_clone: 2013-04-24 10:05:40

what should be the output for wrong cases ?
1)length >200
2)length=200 but stringlength>200
3)string contains non binary chararcters

my solution works for all strings from length 1 to 200, with ease. max time taken is 0.02. however i keep getting wrong answer.

Prashant Mishra: 2012-09-18 16:27:47

can someone tell some test cases... getting WA?

KEVALSAHU: 2012-08-26 01:10:50

simple problem guys :)

Last edit: 2012-08-26 01:15:23
Caesum: 2011-01-28 14:09:12

I think the pieces are 10011,101,1
and for the second 0010111101100

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