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));
}



Measured memory_bytes: 6396