A row of balloons has a number written on each one, given in nums. You burst the balloons one at a time, in any order you choose, until none are left.
Bursting a balloon earns its number times the numbers of its two current neighbours: the nearest balloon still standing on its left and the nearest one still standing on its right. Where there is no balloon left on a side, use 1 for that side. Once a balloon bursts, the balloons on either side of it become neighbours.
Return the most coins you can earn in total.
Bursting 4 first earns 2 × 4 × 3 = 24 and leaves [2,3]. Bursting 2 earns 1 × 2 × 3 = 6, and the last balloon earns 1 × 3 × 1 = 3. That is 33, and no other order earns more.
Burst the 0 first (it earns nothing), then 3 for 1 × 3 × 6 = 18, then 6 for 6. Bursting 3 first would multiply it by the 0 beside it.
A lone balloon has no neighbours, so it earns 1 × 7 × 1.
Constraints
1 <= nums.length <= 1000 <= nums[i] <= 100Deciding which balloon goes first is hard, because every burst changes who neighbours whom. Think instead about which balloon in a stretch goes last: while the others in the stretch burst, it stays put, so the left part and the right part never touch each other.
33