Sane's Monthly Algorithms Challenge: October 2008
Farmer John has just opened up a new daycare for cows of all sizes. The
daycare consists of
There is a mother cow inside every pen whose job is to take care of all
the cows inside it. When there are no cows inside her pen, she does not
need to do any work. For every new cow added to her pen, the amount of
work she has to do to take care of each individual cow is increased by
For example, if she has
The daycare had become so popular that Farmer John is taking in more cows than the mother cows can handle. To fix this, he realizes that he can lessen the number of cows in some pens (and thus the amount of work done by some mothers) by moving smaller cows to the pens built for larger cows.
Only smaller cows can be moved to larger pens (larger cows can not be moved to smaller pens). The size of each cow in a pen does not affect the amount of work done by a mother cow (only the quantity does). Moreover, there is no limit to the number of cows that can be moved inside a pen.
Given the number of cow pens and the number of cows of each size, move the cows around so that the total amount of work done by all of the mother cows is minimal.
Input Specification
The first line of the input consists of a single integer,
The next
Output Specification
Output the sum of all work done by each mother such that it's minimal.
Note that it may be necessary to use a 64-bit integer (long long
in
C/C++, int64
in PAS) to compute your answer.
Sample Input
4
4
1
2
0
Sample Output
13
Explanation of Sample Output
Two of the size


Comments