WOSS Dual Olympiad 2023 Team Round P1: Combining Candy Canes
View as PDFYanzi has  candy canes. The 
th candy cane has a length of 
. She wants to become the ultimate candy master, which means she needs to merge all her candy canes together with her magical power! The energy required to merge two candy canes of lengths 
 and 
 is the minimum of 
 and 
. After, she ends up with a single candy cane of length 
. Yanzi is super lazy, so she wants to know the minimum amount of energy she needs to use to merge all her candy canes into one. Can you find this value for her?
Constraints
Input Specification
The first line contains a single integer .
The second line contains  space-separated integers 
, the lengths of the candy canes.
Output Specification
Output a single integer, the minimum cost to combine all the candy canes together.
Sample Input
5
4 7 3 9 8
Sample Output
22
Explanation
Combine the canes with lengths 3 and 9, costing 3.
Combine the canes with lengths 12 and 7, costing 7.
Combine the canes with lengths 19 and 8, costing 8.
Combine the canes with lengths 27 and 4, costing 4.
Comments