labels[i] is the label of the i-th vertex going clockwise.
A polygon with n vertices must be cut along non-crossing diagonals into n − 2 triangles. A triangle costs the product of its three vertex labels, and the cost of the whole cut is the sum over all triangles.
Return the lowest possible cost.labels=[2, 5, 3] → 30
It is already a triangle: 2 · 5 · 3 = 30.
labels=[1, 6, 2, 6] → 24
The diagonal between the vertices labelled 1 and 2 gives 1 · 6 · 2 + 1 · 2 · 6 = 24; the other diagonal would cost 1 · 6 · 6 + 6 · 2 · 6 = 108.
labels=[4, 1, 4, 1, 4] → 24
labels=[2, 5, 3]
30
Run checks the first 3 tests in your browser. Submit checks all 94 tests on the server, hidden ones included.