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,000
wheren
is length ofnums
.
https://binarysearch.com/problems/One-Integer
Examples
Example 1
Input
- nums =
[1, 2, 3, 4, 5]
Output
- answer =
33
Explanation
- We take
1
and2
out to get[3, 4, 5, 3]
- We take
3
and3
out to get[4, 5, 6]
- We take
4
and5
out to get[6, 9]
-
We take
6
and9
out to get[15]
- The sum is
33 = 1 + 2 + 3 + 3 + 4 + 5 + 6 + 9
Leave a comment