Merlin
InterviewTime limit2sMemory limit512 MB
Given n vessels with amounts of elixir, find the minimum number of vessels to empty (and smash) so the remaining vessels can be made equal by redistributing the poured elixir.
- Level
Medium5 of 10
- Topics
- Greedy, Math, Sorting, Prefix sum
- Solved
- No attempts yet
Problem
One day, returning to his tower, Merlin found that Morgana had cursed all his vessels of the elixir of wisdom.
Merlin knows how to lift the curse, but the corresponding spell requires that all the vessels it is applied to hold an equal amount of elixir.
To achieve this, Merlin decided to act as follows. He chooses several vessels and pours all the elixir from the chosen vessels into the remaining ones. He may distribute the poured elixir among the remaining vessels arbitrarily. After all the elixir from the chosen vessels has been poured, Merlin smashes the emptied vessels (the curse can no longer be lifted from them), throws away the shards, and casts the curse-lifting spell on the remaining vessels.
Help the wizard find the smallest number of vessels he has to smash to lift Morgana's curse.
Input
The first line of the input file contains the number (), the number of vessels. The second line contains numbers (), the number of liters of the elixir of wisdom in each vessel.
Output
Output the minimum number of vessels Merlin has to smash.
Notes
In the first example, for instance, one can pour liters of elixir from the first vessel into the second and liters into the third, then smash the first vessel.
In the second example, the vessels initially contain equal amounts of elixir, so nothing needs to be poured.
In the third example, for instance, one can pour 1 liter of elixir from the first vessel into the second, 2 liters each from the fifth vessel into the second and third, 1 liter from the fifth vessel into the fourth, then smash the first and fifth vessels.