Reprinted please indicate the source: http://blog.csdn.net/u012860063

Question link: http://poj.org/problem? Id = 3253

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs*N*(1 ≤*N*≤ 20,000) planks of wood, each having some integer Length*Li*(1 ≤*Li*≤ 50,000) units. He then purchases a single long board just long enough to saw into*N*Planks (I. e., whose length is the sum of the lengths*Li*). Fj is ignoring the "kerf", the extra length lost to sawdust when a sawcut is made; You shoshould ignore it, too.

FJ sadly realizes that he doesn't own a saw with which to cut the wood, so he mosies over to farmer don's farm with this long board and politely asks if he may borrow a saw.

Farmer Don, a closet capitalist, doesn't lend FJ a saw but instead offers to charge Farmer John for each of*N*-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 21 costs 21 cents.

Farmer Don then lets farmer john decide the order and locations to cut the plank. Help Farmer John determine the minimum amount of money he can spend to create*N*Planks. fj knows that he can cut the board in varous different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 1: One integer

*N*, The number of planks

Lines 2 ..

*N*+ 1: each line contains a single integer describing the length of a needed plank

Output

Line 1: One INTEGER: the minimum amount of money he must spend to make

*N*-1 cuts

Sample Input

3858

Sample output

34

Hint

He wants to cut a board of length 21 into pieces of lengths 8, 5, and 8.

The original Board measures 8 + 5 + 8 = 21. the first cut will cost 21, and shoshould be used to cut the Board into pieces measuring 13 and 8. the second cut will cost 13, and shoshould be used to cut the 13 into 8 and 5. this wocould cost 21 + 13 = 34. if the 21 was cut into 16 and 5 instead, the second cut wocould cost 16 for a total of 37 (which is more than 34 ).

Source

Usaco 2006 November gold

**General question:**

A farmer wants to crop a board into several small boards with a given length. Each saw is charged for the length of the current wood board.

The minimum cost is calculated based on the length of each sub-board and the number n of sub-boards.

**Tip:**

To

3

5 8 5 For example:

First saw a 21-length wooden board from an infinitely long wooden board, and spent 21

Then, we saw 5 boards from a wooden board with a length of 21 and spent 5

Then, we saw 8 boards from a 16-length board and spent 8

Total cost = 21 + 5 + 8 = 34

**Solution:**

Using the Huffman idea, to minimize the total cost, you can only add the two boards with the minimum length each time, and then add the "and" to the total cost.

Although this question uses the Huffman idea, it will time out to directly use the huffmantree. You can use the priority queue to do it.

The Code is as follows:

/* STL priority queue */# include <cstdio> # include <queue> # include <vector> # include <iostream> using namespace STD; int main () {int N; // number of boards to be cut _ int64 temp, A, B, mincost; while (~ Scanf ("% d", & N) {// defines a priority queue from small to large. You can change greater to less, that is, from large to small priority_queue <int, vector <int>, greater <int> q; while (! Q. empty () // clear the queue Q. pop (); For (INT I = 1; I <= N; I ++) {scanf ("% i64d", & temp); q. push (temp); // enter the required length (cost) and join the team} mincost = 0; // The minimum fee is initially zero while (Q. size ()> 1) // when the queue is smaller than or equal to one element, it jumps out of {A = Q. top (); // obtain the value of the first element of the team and enable it to exit Q. pop (); B = Q. top (); // take the first line twice, that is, obtain the minimum two values Q. pop (); q. push (a + B); // put the two smallest elements into the queue mincost + = a + B;} printf ("% i64d \ n", mincost );} return 0 ;}