All problems

Problem 960

Minimum Score Triangulation of Polygon

Medium · 20 pts · Array, Dynamic Programming

Problem

A convex polygon has N vertices with integer values, listed in clockwise order. Triangulate it into N - 2 triangles; each triangle scores the product of its three vertex values, and the score of a triangulation is the total. Find the minimum total score. Interval DP: for vertices i < j, the edge i-j belongs to a triangle with some k between them, so best(i, j) = min over k of best(i, k) + best(k, j) + V[i] × V[k] × V[j].

Input

The first line contains an integer N, the size of the array. The second line contains N space-separated integers A[0], A[1], ..., A[N-1].

Output

Print the minimum triangulation score.

Constraints

3 <= N <= 50; 1 <= V[i] <= 100

Example input

4
3 7 4 5

Example output

144

Explanation

For 3 7 4 5, triangulating with triangles (3,7,5) and (7,4,5) scores 105 + 140 = 245, while (3,7,4) and (3,4,5) score 84 + 60 = 144. The minimum is 144.

Every problem here can be used in a RelayTrixCode team match. Create a room.

Previous: Filling Bookcase Shelves Next: Guess Number Higher or Lower II