## SMAC 08/09 (Oct) #4 - Daycare

View as PDF

Points: 15 (partial)
Time limit: 0.6s
Memory limit: 64M

Problem type
##### 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 cow pens. Each cow pen is built differently to accommodate cows of a unique size from to .

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 cow she does piece of work. If she has cows she does pieces of work. cows is pieces of work. So on and so forth.

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, , representing the number of uniquely sized cow pens.

The next lines that follow will each contain a non-negative integer on the th line, representing the number of cows that are size . There will be one test case where cows rule the planet and each integer may be as large as .

#### 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 cows are moved to pen and another is moved to pen . The size cow and one of the size cows move to pen . The mothers in , , and each do four pieces of work while the mother in pen does piece of work. . While there are other equally optimal placements, there exist none better than .

Before (Total Work = 21)
After (Total Work = 13)
Mother cows have not been included in these diagrams.