Problem:
Score : 100 points

Problem Statement

There are N slimes lining up in a row.
Initially, the i-th slime from the left has a size of a_i.

Taro is trying to combine all the slimes into a larger slime.
He will perform the following operation repeatedly until there is only one slime:

Choose two adjacent slimes, and combine them into a new slime. The new slime has a size of x + y, where x and y are the sizes of the slimes before combining them. Here, a cost of x + y is incurred. The positional relationship of the slimes does not change while combining slimes.

Find the minimum possible total cost incurred.

Constraints

All values in input are integers.

2 \leq N \leq 400

1 \leq a_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
a_1 a_2 \ldots a_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 20 30 40

Sample Output 1

190

Taro should do as follows (slimes being combined are shown in bold):

(10, 20, 30, 40) → (30, 30, 40)

(30, 30, 40) → (60, 40)

(60, 40) → (100)

Sample Input 2

5
10 10 10 10 10

Sample Output 2

120

Taro should do, for example, as follows:

(10, 10, 10, 10, 10) → (20, 10, 10, 10)

(20, 10, 10, 10) → (20, 20, 10)

(20, 20, 10) → (20, 30)

(20, 30) → (50)

Sample Input 3

3
1000000000 1000000000 1000000000

Sample Output 3

5000000000

The answer may not fit into a 32-bit integer type.

Sample Input 4

6
7 6 8 6 1 1

Sample Output 4

68

Taro should do, for example, as follows:

(7, 6, 8, 6, 1, 1) → (7, 6, 8, 6, 2)

(7, 6, 8, 6, 2) → (7, 6, 8, 8)

(7, 6, 8, 8) → (13, 8, 8)

(13, 8, 8) → (13, 16)

(13, 16) → (29)

Solution in Rust (1.15.1) (judge verdict: Accepted)

macro_rules! debug {
    ( $x:expr ) => {
        if cfg!(debug_assertions) {
            println!("{}:{} {} = {:?}", line!(), column!(), stringify!($x), $x);
        }
    }
}

mod my {
    pub mod io {
        use std::io::{BufRead, ErrorKind, Result};
        use std::str::{FromStr, from_utf8};
        use std::fmt::Debug;

        pub fn read_word<R: BufRead>(r: &mut R, buf: &mut Vec<u8>)
                                              -> Result<usize> {
            let mut read = 0;
            loop {
                let (done, used) = {
                    let available = match r.fill_buf() {
                        Ok(n) => n,
                        Err(ref e) if e.kind() == ErrorKind::Interrupted => continue,
                        Err(e) => return Err(e)
                    };
                    match available.iter().position(|&x| x == b'\n' || x == b' ') {
                        Some(i) => {
                            buf.extend_from_slice(&available[..i]);
                            (true, i + 1)
                        }
                        None => {
                            buf.extend_from_slice(available);
                            (false, available.len())
                        }
                    }
                };
                r.consume(used);
                read += used;
                if done || used == 0 {
                    return Ok(read);
                }
            }
        }

        pub struct InputReader<R: BufRead> {
            reader: R,
            buf: Vec<u8>,
        }

        impl<R: BufRead> InputReader<R> {
            pub fn new(reader: R) -> InputReader<R> {
                InputReader {
                    reader: reader,
                    buf: Vec::<u8>::new(),
                }
            }

            pub fn read_word(&mut self) {
                read_word(&mut self.reader, &mut self.buf);
            }
            
            pub fn read<T>(&mut self) -> T
                where T: FromStr,
                      T::Err: Debug
            {
                self.buf.clear();
                self.read_word();
                from_utf8(&self.buf).unwrap().trim().parse().unwrap()
            }
        }
    }
}

fn optimal_binary_search_tree(w: &[u64]) -> u64 {
    let n = w.len();
    let mut p = vec![vec![0; n]; n];
    let mut c = vec![vec![0; n]; n];
    for k in 0..n {
        p[k][k] = k;
        c[k][k] = 0;
    }

    let mut acc = vec![0; n + 1];
    acc[0] = 0;
    for i in 0..n {
        acc[i + 1] = acc[i] + w[i];
        //println!("acc[{}] = {}", i + 1, acc[i + 1]);
    }

    for d in 1..n {
        for i in 0..n - d {
            let j = i + d;
            //println!("(i, j) = ({}, {})", i, j);
            let mut c_min = std::u64::MAX;
            for k in p[i][j - 1]..std::cmp::min(j, p[i + 1][j] + 1) {
            //for k in i..j {
                //println!("c[{}][{}] + c[{}][{}] = {} + {} = {}", i, k, k + 1, j, c[i][k], c[k + 1][j], c[i][k] + c[k + 1][j]);
                if c_min > c[i][k] + c[k + 1][j] {
                    p[i][j] = k;
                    c_min = c[i][k] + c[k + 1][j];
                }
            }
            c[i][j] = c_min + acc[j + 1] - acc[i];
            //println!("p[{}][{}] = {}", i, j, p[i][j]);
            //println!("c[{}][{}] = {}", i, j, c[i][j]);
        }
    }
    c[0][n - 1]
}

fn main() {
    let stdin = std::io::stdin();
    let mut stdin = my::io::InputReader::new(stdin.lock());

    let n = stdin.read();
    let mut w = vec![0; n];
    for i in 0..n {
        w[i] = stdin.read();
    }
    println!("{}", optimal_binary_search_tree(&w));
}