Problem 198
Burst Balloons
Hard · 30 pts · Array, Dynamic Programming, Interval DP
Problem
N balloons stand in a row, and balloon i shows the number A[i]. When you burst a balloon, you earn left × A[i] × right coins, where left and right are the numbers on its current neighbours; a missing neighbour (at either end) counts as 1. After a balloon bursts, its two neighbours become adjacent. Burst all the balloons in the best order and print the maximum total coins. Think about which balloon is burst last in a range: if balloon k is the last one burst between boundaries i and j, it earns A[i] × A[k] × A[j], and the two sides are independent. This gives an O(N³) interval DP.
Input
First line: integer N. Second line: N space-separated integers.
Output
Print the maximum coins.
Constraints
1 <= N <= 300; 0 <= A[i] <= 100
Example input
4 3 1 5 8
Example output
167
Explanation
For 3 1 5 8: burst 1 first (3 × 1 × 5 = 15), then 5 (3 × 5 × 8 = 120), then 3 (1 × 3 × 8 = 24), and finally 8 (1 × 8 × 1 = 8). The total is 15 + 120 + 24 + 8 = 167, the maximum.