Standing Ovation (Large)
InterviewTime limit5sMemory limit512 MB
Find the fewest friends to add so every audience member stands as each shyness threshold is met in order.
- Level
Easy3 of 10
- Topics
- Greedy
- Solved
- No attempts yet
Problem
It is opening night at the opera, and your friend sings the prima donna, the lead female role. You will not be in the audience, but you want her to get a standing ovation, meaning every audience member stands up and claps for her.
At the start the whole audience is seated. Every audience member has a shyness level. An audience member with shyness level waits until at least other audience members are already standing and clapping, and stands up to clap the moment that happens. An audience member with always stands up and claps right away, no matter what anyone else does. For example, an audience member with is seated at the beginning and stands up later, after seeing at least two other people standing and clapping.
You know the shyness level of everyone in the audience, and you can invite extra friends of the prima donna into the audience so that everyone ends up standing. You may give each invited friend any shyness level you want, and those levels need not be equal. Find the minimum number of friends you have to invite to guarantee a standing ovation.
Input
The first line contains the number of test cases . Each of the following test cases is one line holding , the shyness level of the shyest person in the audience, then a space, then a string of digits. The th digit of that string (counting from 0) is how many audience members have shyness level . For example, the string "409" means that four audience members have and nine have , with nobody at any other level. The number of people at each shyness level is always between 0 and 9.
The string never ends in a 0, so the audience always holds at least one person.
Limits
Output
For each test case, print one line in the form "Case #x: y", where is the test case number starting from 1 and is the minimum number of friends you must invite.
Hint
In case 1 of the first example the audience produces a standing ovation on its own, with no invitation. The member with stands first, then the member with stands, and so on.
In case 2 you must invite one friend with , and that is enough to get the whole audience standing.
In case 3 one optimal answer adds two audience members with .
In case 4 there is only one audience member and he stands up immediately, so no friend is needed.