cho.sh
NotesCho Mini
Loading...

Arranging Cargo

Time limit

1s

Memory limit

128 MB

Problem

There are N cargo slots in a row, and each slot contains one item with a distinct weight. The slots must be rearranged so that the lightest item is at the front, followed by the remaining items in increasing order of weight. In one operation, you swap the positions of two items; the force required for that operation is the sum of their weights.

Given the number of slots and the current weights from front to back, compute the minimum total force needed to finish the arrangement.

For example, suppose four items with weights 10, 2, 8, and 5 are placed in that order. If you swap the item of weight 10 with the item of weight 2, then swap the item of weight 10 with the item of weight 5, the arrangement is complete. The total force is (10+2) + (10+5) = 27, as shown in Figure 1.

On the other hand, you can first swap the items of weights 2 and 5, then swap the items of weights 10 and 2. This also completes the arrangement, and the total force is (2+5) + (10+2) = 19, as shown in Figure 2.

Input

The first line contains the number of cargo slots, N. Each of the next N lines contains the weight of one item, in order from front to back.

N is a positive integer at most 1,000. Each weight is a positive integer at most 10,000. All weights are distinct.

Output

Print the minimum force needed to arrange the cargo slots.