LearnPy Hub
← Arena
AdvancedRating 1800

Cheapest Tree From Leaves

Build a binary tree whose leaves, read left to right, are the given numbers; every internal node has two children and its value is (largest leaf on its left) × (largest leaf on its right). Print the smallest possible sum of the internal node values.

Input

n (≥ 2), then n positive integers.

Output

One integer.

Sample 1

Input
3
6 2 4
Output
32

Sample 2

Input
2
4 11
Output
44
solution.py
Output appears here
Cheapest Tree From Leaves · Python Arena problem · LearnPy Hub