One Integer
You are given a list of integers nums. You can reduce the length of nums by taking any two integers, removing them, and appending their sum to the end. The cost of doing this is the sum of the two integers you removed.
Return the minimum total cost of reducing nums to one integer.
Constraints
n ≤ 100,000wherenis length ofnums.
https://binarysearch.com/problems/One-Integer
Examples
Example 1
Input
- nums =
[1, 2, 3, 4, 5]
Output
- answer =
33
Explanation
- We take
1and2out to get[3, 4, 5, 3] - We take
3and3out to get[4, 5, 6] - We take
4and5out to get[6, 9] -
We take
6and9out to get[15] - The sum is
33 = 1 + 2 + 3 + 3 + 4 + 5 + 6 + 9
Leave a comment