Choose an order to burst balloons to maximize the total coins gained.
You are given an array nums where each element represents a balloon with a positive integer value.
When you burst a balloon at index i, you gain coins equal to the product of the values of its nearest remaining neighbors on the left and right, multiplied by nums[i] itself. If one side has no remaining balloon, treat that missing value as 1.
Your task is to determine the maximum number of coins you can collect by bursting all balloons in an optimal order.
Because the neighbors change after every burst, the order matters.
1 at both ends.nums.1 <= nums.lengthnums[i] > 0Example 1
Input
nums = [3,1,5,8]
Output
167
Explanation
One optimal order is to burst 1, then 5, then 3, then 8. With virtual balloons of value 1 at both ends, the best total is 167.
Premium problem context
Premium adds guided hints, editorial links, similar variants, discussion resources, and concept maps so you can understand why a problem matters, not just solve it once.