Fields X82108


Statement
 

pdf   zip

In the Middle Ages, many villages practiced agriculture using the open-field system: each field was divided into many narrow strips of land, each cultivated by a different family.

The government of Utopia has decided to reform agriculture and cultivate each field using modern equipment. To compensate the families of each village, the government has agreed to make a cash payment to each village. The amount of the cash payment is calculated using the procedure described below.

For simplicity, assume that the strips of land are all vertical. Given a field with nn strips of land, let l0,l1,,lnl_0,l_1,\ldots,l_n be the lengths of their borders, where li1l_{i-1} is the length of the left border of strip ii, and lil_i is the length of the right border. Note that the right border of strip i1i-1 is also the left border of strip ii.

When merging two adjoining strips of land i1i-1 and ii, the cash payment is li1×li×li+1l_{i-1}\times l_i\times l_{i+1}, i.e. the product of the lengths of the three borders of i1i-1 and ii (including the common border that separates them). After merging the two strips of land, they become a single strip whose borders have lengths li1l_{i-1} and li+1l_{i+1}.

Your goal is to help the villagers maximize the compensatory cash payment.

Input

The input starts with the number of test cases T100T \leq 100. For each test case, there is an intenger n100n \leq 100 that represents the number of strips of land in a given field. On the next line, there are n+1n+1 numbers l0,l1,,lnl_0,l_1,\ldots,l_n describing the lengths of the borders.

Output

For each test case, print the maximum cash payment CC for the given field on a separate line.

In the first example, there are three strips of land 11, 22 and 33. There are two ways to merge the strips: first merge 11 and 22, and then merge the resulting strip with 33, or first merge 22 and 33, and then merge the resulting strip with 11. The cash payment for the first case is 1×4×3+1×3×2=201\times 4 \times 3 + 1\times 3\times 2 = 20, while the cash payment for the second case is 4×3×2+1×4×2=324\times 3\times 2 + 1\times 4\times 2 = 32. Hence the second case is optimal.

In the second example, there are four strips of land. The maximum cash payment is achieved by merging 22 with 33, then merging the result with 11, and finally merging the result with 44, resulting in a cash payment of 3×2×3+4×3×3+4×3×4=1023\times 2\times 3 + 4\times 3\times 3 + 4\times 3\times 4 = 102.

Public test cases
  • Input

    2
    3
    1 4 3 2
    4
    4 3 2 3 4
    

    Output

    32
    102
    
  • Information
    Author
    Anders Jonsson
    Language
    English
    Official solutions
    C++
    User solutions
    C++