You are following a prescribed training for weightlifting. The training consists of a series of exercises that you must do in order. Each exercise requires a specific set of weights to be placed on a machine.
There are

The weights are placed on the machine as a stack. Formally, with a single operation, you can either add a new weight of any type to the top of the stack, or remove the weight that is currently at the top of the stack.
You can load the weights for each exercise onto the machine's stack in any order. So, if you place the weight of type B at the bottom in the first exercise of the example above, you will have to take all the weights off before putting on the weights for the second exercise. On the other hand, if you place the weight of type B third from the bottom, you can leave two of the weights of type A on the bottom of the stack to be part of the next exercise's set, saving you some time.
Given the amount of weights of each type needed for each exercise, find the minimum number of operations needed to do them all. You must complete the exercises in the order given. The machine stack starts out empty, and you must leave it empty after you finish with all your exercises.
Input Specification
The first line of the input gives the number of test cases,
Output Specification
For each test case, output one line containing Case #x: y
,
where
Limits
Time limit: 20 seconds.
Memory limit: 1 GB.
Test Set 1
Test Set 2
Sample Input
3
3 1
1
2
1
2 3
1 2 1
2 1 2
3 3
3 1 1
3 3 3
2 3 3
Sample Output
Case #1: 4
Case #2: 12
Case #3: 20
In Sample Case #1, there is only one type of weight.
The first exercise needs
- Add a weight onto the stack. You do the first exercise.
- Add a weight onto the stack. You do the second exercise.
- Remove a weight from the top of the stack. You do the third exercise.
- Remove a weight from the top of the stack. Now the stack becomes empty.
In Sample Case #2, one way to complete the exercises in
- Add a weight of type
. - Add a weight of type
. - Add a weight of type
. - Add a weight of type
. Now the stack contains weights of types from bottom to top. You do the first exercise. - Remove a weight of type
from the top of the stack. - Add a weight of type
. - Add a weight of type
. Now the stack contains weights of types from bottom to top. You do the second exercise. - Remove a weight of type
from the top of the stack. - Remove a weight of type
from the top of the stack. - Remove a weight of type
from the top of the stack. - Remove a weight of type
from the top of the stack. - Remove a weight of type
from the top of the stack. Now the stack becomes empty.
Comments