Balance Scale
InterviewTime limit1sMemory limit1024 MB
Stones are placed on a balance by a fixed rule, and the task asks for the fewest weights from 1g to 100g needed to level the final imbalance.
- Level
Easy3 of 10
- Topics
- Simulation, Greedy
- Solved
- No attempts yet
Problem
There are stones numbered from to . Place them on a two-pan balance as follows.
- Put stone on the left pan and stone on the right pan.
- For each stone , in order, do one of the following.
- If the balance is level, put stone on the left pan.
- If the balance is not level, put stone on the lighter pan.
After all stones are placed by the rules above, the balance may still not be level. In that case, add weights to the lighter pan to level it. There are 7 kinds of weights: 1g, 2g, 5g, 10g, 20g, 50g, and 100g. There is no limit on the number of weights of each kind.
For the stones given in the input, find the minimum number of additional weights needed to level the balance after placing the stones by the rules above, and write a program that prints it.
Input
Standard input is used. The first line contains a positive integer (), the number of stones. The next line contains integers, the weights of the stones in numbering order. Each stone weighs at least , and the total weight of all stones is at most .
Output
Standard output is used. Print the minimum number of additional weights needed to level the balance on one line.