```python n = int(input()) b = list(map(int, input().split()))
dp = [[0] * n for _ in range(n)]
for j in range(2, n): for i in range(j-2, -1, -1): dp[i][j] = float('inf') for k in range(i+1, j): penalty = b[k] * (b[i] + b[j]) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j] + penalty)