#K91467. Minimum Potion Mixing Cost
Minimum Potion Mixing Cost
Minimum Potion Mixing Cost
You are given n potions, each with an effect value. The goal is to mix these potions into one potion with minimum total cost. Each time, you can combine any two potions. The cost to mix two potions is equal to the sum of their effect values, and the resulting potion has an effect value equal to that sum. You need to perform a series of mixings until only one potion remains. The total cost is the sum of all individual mixing costs.
This problem can be mathematically formulated as follows. Given a set of potion effect values \(a_1, a_2, \dots, a_n\), you must choose a sequence of pairwise mixes so that the cumulative cost is minimized. The cost operation is defined as:
[ cost = a_i + a_j ]
Input Constraints:
- \(1 \leq n \leq 10^5\)
- \(1 \leq a_i \leq 10^9\)
Example:
Input: 4 4 3 2 6</p>Output: 29
Explanation: One optimal sequence is to mix 2 and 3 (cost=5), then mix the result with 4 (cost=9), and finally mix that with 6 (cost=15). The total cost is \(5 + 9 + 15 = 29\).
inputFormat
The input is read from standard input and consists of two lines:
- The first line contains an integer n, representing the number of potions.
- The second line contains n space-separated integers, where each integer denotes the effect value of a potion.
outputFormat
Output a single integer, which is the minimum total cost to mix all the potions into one. The result should be printed to standard output.
## sample4
4 3 2 6
29