Problem:
Score : 1000 points

Problem Statement

There is a stack of N cards, each of which has a non-negative integer written on it. The integer written on the i-th card from the top is A_i.

Snuke will repeat the following operation until two cards remain:

Choose three consecutive cards from the stack.

Eat the middle card of the three.

For each of the other two cards, replace the integer written on it by the sum of that integer and the integer written on the card eaten.

Return the two cards to the original position in the stack, without swapping them.

Find the minimum possible sum of the integers written on the last two cards remaining.

Constraints

2 \leq N \leq 18

0 \leq A_i \leq 10^9 (1\leq i\leq N)

All values in input are integers.

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 ... A_N

Output

Print the minimum possible sum of the integers written on the last two cards remaining.

Sample Input 1

4
3 1 4 2

Sample Output 1

16

We can minimize the sum of the integers written on the last two cards remaining by doing as follows:

Initially, the integers written on the cards are 3, 1, 4, and 2 from top to bottom.

Choose the first, second, and third card from the top. Eat the second card with 1 written on it, add 1 to each of the other two cards, and return them to the original position in the stack. The integers written on the cards are now 4, 5, and 2 from top to bottom.

Choose the first, second, and third card from the top. Eat the second card with 5 written on it, add 5 to each of the other two cards, and return them to the original position in the stack. The integers written on the cards are now 9 and 7 from top to bottom.

The sum of the integers written on the last two cards remaining is 16.

Sample Input 2

6
5 2 4 1 6 9

Sample Output 2

51

Sample Input 3

10
3 1 4 1 5 9 2 6 5 3

Sample Output 3

115

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Wrong Answer)

import java.util.Scanner;

//https://atcoder.jp/contests/agc035/tasks/agc035_d
public class Main {
    private static long[] a;

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        a = new long[n];
        for (int i = 0; i < n; i++) {
            a[i] = sc.nextInt();
        }
        System.out.println(solve(0, n, 1, 1));
    }

    /*
    reduce the interval of a into 2 cards. What is the minimum value of the left card
    multiplied by leftCoef, plus the right c card multiplied by rightCoef
     */
    private static long solve(int l, int r, int leftCoef, int rightCoef) {
        assert l + 1 < r;
        if (l + 2 == r) {
            return a[l] * leftCoef + a[r - 1] * rightCoef;
        }
        long ans = Long.MAX_VALUE;
        for (int mid = l + 1; mid < r - 1; mid++) {
            long bestSolutionThisMid;
            if (mid - l == 1 && r - mid == 2) {
                bestSolutionThisMid = leftCoef * (a[l] + a[mid]) + rightCoef * (a[mid] + a[r - 1]);
            } else if (mid - l == 1 && r - mid != 2) {
                bestSolutionThisMid = leftCoef * a[l] + leftCoef * 2 * a[mid] + rightCoef * a[mid] +
                        +solve(mid + 1, r, leftCoef + rightCoef, rightCoef);
            } else if (mid - l != 1 && r - mid == 2) {
                bestSolutionThisMid = solve(l, mid, leftCoef, leftCoef + rightCoef) +
                        leftCoef * a[mid] + rightCoef * 2 * a[mid] + rightCoef * a[r - 1];
            } else {
                bestSolutionThisMid = solve(l, mid, leftCoef, 2 * leftCoef + rightCoef) +
                        3 * leftCoef * a[mid] + 2 * rightCoef * a[mid] +
                        solve(mid + 1, r, leftCoef + rightCoef, rightCoef);

                bestSolutionThisMid = Math.min(bestSolutionThisMid,
                        solve(l, mid, leftCoef, leftCoef + rightCoef)
                                + 2 * leftCoef * a[mid] + 3 * rightCoef * a[mid]
                                + solve(mid + 1, r, leftCoef + 2 * rightCoef, rightCoef));
            }
            ans = Math.min(bestSolutionThisMid, ans);
        }
        return ans;
    }
}