It's opening night at the opera, and your friend is the prima donna (the lead female singer). You will not be in the audience, but you want to make sure she receives a standing ovation -- with every audience member standing up and clapping their hands for her.
Initially, the entire audience is seated. Everyone in the audience has a shyness level. An audience member with shyness level will wait until at least other audience members have already stood up to clap, and if so, she will immediately stand up and clap. If , then the audience member will always stand up and clap immediately, regardless of what anyone else does. For example, an audience member with will be seated at the beginning, but will stand up to clap later after she sees at least two other people standing and clapping.
You know the shyness level of everyone in the audience, and you are prepared to invite additional friends of the prima donna to be in the audience to ensure that everyone in the crowd stands up and claps in the end. Each of these friends may have any shyness value that you wish, not necessarily the same. What is the minimum number of friends that you need to invite to guarantee a standing ovation?
Input Specification
The first line of the input gives the number of test cases, . test cases follow. Each consists of one line with , the maximum shyness level of the shyest person in the audience, followed by a string of single digits. The th digit of this string (counting starting from 0) represents how many people in the audience have shyness level . For example, the string 409
would mean that there were four audience members with and nine audience members with (and none with or any other value). Note that there will initially always be between 0 and 9 people with each shyness level.
The string will never end in a 0
. Note that this implies that there will always be at least one person in the audience.
Output Specification
For each test case, output one line containing Case #x: y
, where is the test case number (starting from 1) and is the minimum number of friends you must invite.
Limits
Memory limit: 1 GB.
.
Small dataset
Time limit: 30 seconds.
.
Large dataset
Time limit: 60 seconds.
.
Sample Input
4
4 11111
1 09
5 110011
0 1
Sample Output
Case #1: 0
Case #2: 1
Case #3: 2
Case #4: 0
In Case #1, the audience will eventually produce a standing ovation on its own, without you needing to add anyone -- first the audience member with will stand up, then the audience member with will stand up, etc.
In Case #2, a friend with must be invited, but that is enough to get the entire audience to stand up.
In Case #3, one optimal solution is to add two audience members with .
In Case #4, there is only one audience member and he will stand up immediately. No friends need to be invited.
Note
This problem has different time limits for different batches. If you exceed the Time Limit for any batch, the judge will incorrectly display >60.000s
regardless of the actual time taken. Refer to the Limits section for batch-specific time limits.
This problem originally had a much higher time limit. However, as reference solutions were much faster, the Time Limit was been reduced accordingly.
Comments