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 Rust (1.15.1) (judge verdict: Accepted)

// ${url}
//
#![allow(unused_imports)]
use std::io::*;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        println!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

fn next_permutation(a: &mut Vec<usize>) -> bool {
    let n = a.len();
    let mut x = n - 2;
    while a[x] >= a[x+1] {
        if x == 0 {
            return false;
        }
        x -= 1;
    }

    let mut y = n - 1;
    while y > x && a[y] <= a[x] {
        y -= 1;
    }
    let tmp = a[x];
    a[x] = a[y];
    a[y] = tmp;

    let (ll, rr) = a.split_at_mut(x+1);
    let (md, rr) = rr.split_at_mut(n-x-1);
    md.sort();
    true
}

fn process(a: Vec<usize>, mut d: Vec<Vec<i32>>) {
    let n = d.len();
    let mut done = 0;
    for ai in a {
        if ai == 0 || ai == n-1 {
            continue;
        }
        let mut f = ai-1;
        while done & (1<<f) >= 1 {
            f -= 1;
        }
        for i in 0..n {
            d[f][i] += d[ai][i];
        }
        let mut f = ai+1;
        while done & (1<<f) >= 1 {
            f += 1;
        }
        for i in 0..n {
            d[f][i] += d[ai][i];
        }
        done |= 1<<ai;
    }
    for i in 0..n {
        d[0][i] += d[n-1][i];
    }
    debug!(d[0]);
}

type Memo = Vec<Vec<HashMap<(i64, i64), i64>>>;

const INF: i64 = 1e18 as i64;

fn solve(l: usize, r: usize, ln: i64, rn: i64, a: &Vec<i64>, memo: &mut Memo) -> i64 {
    let w = (ln, rn);
    if let Some(&v) = memo[l][r].get(&w) {
        return v;
    }
    if l+1 == r {
        memo[l][r].insert((ln, rn), 0);
        return 0;
    }
    let mut best = INF;
    for i in l+1..r {
        let w = ln+rn;
        let left = solve(l, i, ln, w, a, memo);
        let right = solve(i, r, w, rn, a, memo);
        best = min(best, a[i]*w + left + right);
    }
    memo[l][r].insert((ln, rn), best);
    best
}

fn main() {
    input! {
        n: usize, a: [i64; n]
    };

    let mut memo = dvec!(HashMap::new(); n, n);
    let ans = solve(0, n-1, 1, 1, &a, &mut memo);

    println!("{}", ans + a[0] + a[n-1]);
}