Enter \looparrowright

Description

Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs NN (1N20000)(1 ≤ N ≤ 20000) planks of wood, each having some integer length Li(1Li50000)L_i (1 ≤ L_i ≤ 50000) units. He then purchases a single long board just long enough to saw into the NN planks (i.e., whose length is the sum of the lengths LiL_i). FJ is ignoring the “kerf”, the extra length lost to sawdust when a sawcut is made; you should 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 the N1N-1 cuts in the plank. The charge to cut a piece of wood is exactly equal to its length. Cutting a plank of length 2121 costs 2121 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 the N planks. FJ knows that he can cut the board in various different orders which will result in different charges since the resulting intermediate planks are of different lengths.

Input

Line 11: One integer NN, the number of planks
Lines 22 to N+1N+1: Each line contains a single integer describing the length of a needed plank

Output

Line 11: One integer: the minimum amount of money he must spend to make N1N-1 cuts

Sample Input

3
8
5
8

Sample Output

34

Hint

He wants to cut a board of length 2121 into pieces of lengths 8,58, 5, and 88.
The original board measures 8+5+8=218+5+8=21. The first cut will cost 2121, and should be used to cut the board into pieces measuring 1313 and 88. The second cut will cost 1313, and should be used to cut the 1313 into 88 and 55. This would cost 21+13=3421+13=34. If the 2121 was cut into 1616 and 55 instead, the second cut would cost 1616 for a total of 3737 (which is more than 3434).

Translation

我们要将将一根很长的木条切成 NN 份,每份的长度为 LiL_i ,未切割前木条的长度为 i=1NLi\sum\limits_{i=1}^NL_i。每次切断木条时,需要的开销为这根木条的长度,如将一根长度为 2121 的木条分割成 xx21x21-x 的两部分,开销为 21x+x=2121-x+x=21 。请求出按照目标要求将模板切割完最小的开销是多少。

Idea

如果我们需要将一根木条分成长度为 a,ba,b 的两份,那么开销就是 a+ba+b,我们可以定义将两根长度为 a,ba,b 的木条拼成一根的开销也为 a+ba+b 。对于题目的情境,每一次将某一根木条切成两根的开销,相当于将两根木条合并成一根。因此,可以将题目看成是把 NN 段木条拼成一根,每次拼的代价为所拼木条的长度和。
显然,越早拼好的木条其产生开销的机会就越多。例如有三根木条长度分别为 a,b,ca,b,c,如果选择先拼 a,ba,b,那么产生的总开销为 (a+b)+[(a+b)+c]=2a+2b+c(a+b)+[(a+b)+c]=2a+2b+c 。因此贪心的策略是每次选取两个最小的来拼,可以用优先队列实现。时间复杂度为 O(nlogn)O(n\log n)

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<queue>
#include<cstdio>
#define ll long long
using namespace std;
int main()
{
int n;
cin>>n;
priority_queue<ll,vector<ll>,greater<ll> >q;
int i;
for(i=1;i<=n;i++)
{
ll temp;
scanf("%lld",&temp);
q.push(temp);
}
ll ans=0;
for(i=1;i<=n-1;i++)//切n-1刀,进行n-1次拼接
{
//每次取最短的两根
ll first_minimun=q.top();
q.pop();
ll second_minimun=q.top();
q.pop();
ans=ans+first_minimun+second_minimun;
q.push(first_minimun+second_minimun);//重新推进队列
}
cout<<ans<<endl;
return 0;
}

Postscript

此题与 Luogu P1090\text{Luogu}\ \text{P1090} 及其相似。