There are n balloons in a row, and balloon i has the number nums[i] painted on it. You burst them one at a time until none are left.
Bursting a balloon earns left × middle × right coins, where middle is the number on that balloon and left and right are the numbers on the balloons currently next to it. If there is no balloon on one side, use 1 for that side.
Once a balloon bursts, the balloons on either side of it become neighbors. Return the maximum number of coins you can collect.
Examples
Example 1
Input: nums = [3,1,5,8]
Output: 167
The total is 15 + 120 + 24 + 8 = 167.
Example 2
Input: nums = [1,5]
Output: 10
Burst 1 first for 1 × 1 × 5 = 5, then 5 for 1 × 5 × 1 = 5. Bursting 5 first only earns 5 + 1 = 6.
Example 3
Input: nums = [7]
Output: 7
There are no neighbors on either side, so the only burst earns 1 × 7 × 1 = 7.
Example 4
Input: nums = [0,5,0]
Output: 5
Burst both 0s first. They earn nothing, but then 5 is alone and earns 1 × 5 × 1 = 5. Bursting 5 while the 0s are still next to it earns 0 × 5 × 0 = 0.
Example 5
Input: nums = [2,4,8]
Output: 88
Burst 4 first for 2 × 4 × 8 = 64, then 2 for 1 × 2 × 8 = 16, then 8 for 1 × 8 × 1 = 8. 64 + 16 + 8 = 88.
Constraints
1 <= nums.length <= 1000 <= nums[i] <= 100