/**
 *  _           _                 __                            _   _ _   _                                 _                    _                  _
 * | |         | |               / /                           | | (_) | (_)                               | |                  (_)                | |
 * | |__   __ _| |_ ___   ___   / /__ ___  _ __ ___  _ __   ___| |_ _| |_ ___   _____ ______ _ __ _   _ ___| |_ ______ ___ _ __  _ _ __  _ __   ___| |_ ___
 * | '_ \ / _` | __/ _ \ / _ \ / / __/ _ \| '_ ` _ \| '_ \ / _ \ __| | __| \ \ / / _ \______| '__| | | / __| __|______/ __| '_ \| | '_ \| '_ \ / _ \ __/ __|
 * | | | | (_| | || (_) | (_) / / (_| (_) | | | | | | |_) |  __/ |_| | |_| |\ V /  __/      | |  | |_| \__ \ |_       \__ \ | | | | |_) | |_) |  __/ |_\__ \
 * |_| |_|\__,_|\__\___/ \___/_/ \___\___/|_| |_| |_| .__/ \___|\__|_|\__|_| \_/ \___|      |_|   \__,_|___/\__|      |___/_| |_|_| .__/| .__/ \___|\__|___/
 *                                                  | |                                                                           | |   | |
 *                                                  |_|                                                                           |_|   |_|
 *
 * https://github.com/hatoo/competitive-rust-snippets
 */
#[allow(unused_imports)]
use std::cmp::{max, min, Ordering};
#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};
#[allow(unused_imports)]
use std::iter::FromIterator;
#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};
mod util {
    use std::io::{stdin, stdout, BufWriter, StdoutLock};
    use std::str::FromStr;
    use std::fmt::Debug;
    #[allow(dead_code)]
    pub fn line() -> String {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.trim().to_string()
    }
    #[allow(dead_code)]
    pub fn chars() -> Vec<char> {
        line().chars().collect()
    }
    #[allow(dead_code)]
    pub fn gets<T: FromStr>() -> Vec<T>
    where
        <T as FromStr>::Err: Debug,
    {
        let mut line: String = String::new();
        stdin().read_line(&mut line).unwrap();
        line.split_whitespace()
            .map(|t| t.parse().unwrap())
            .collect()
    }
    #[allow(dead_code)]
    pub fn with_bufwriter<F: FnOnce(BufWriter<StdoutLock>) -> ()>(f: F) {
        let out = stdout();
        let writer = BufWriter::new(out.lock());
        f(writer)
    }
}
#[allow(unused_macros)]
macro_rules ! get { ( $ t : ty ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . trim ( ) . parse ::<$ t > ( ) . unwrap ( ) } } ; ( $ ( $ t : ty ) ,* ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; let mut iter = line . split_whitespace ( ) ; ( $ ( iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) , ) * ) } } ; ( $ t : ty ; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ) ) . collect ::< Vec < _ >> ( ) } ; ( $ ( $ t : ty ) ,*; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ ( $ t ) ,* ) ) . collect ::< Vec < _ >> ( ) } ; ( $ t : ty ;; ) => { { let mut line : String = String :: new ( ) ; stdin ( ) . read_line ( & mut line ) . unwrap ( ) ; line . split_whitespace ( ) . map ( | t | t . parse ::<$ t > ( ) . unwrap ( ) ) . collect ::< Vec < _ >> ( ) } } ; ( $ t : ty ;; $ n : expr ) => { ( 0 ..$ n ) . map ( | _ | get ! ( $ t ;; ) ) . collect ::< Vec < _ >> ( ) } ; }
#[allow(unused_macros)]
macro_rules ! debug { ( $ ( $ a : expr ) ,* ) => { println ! ( concat ! ( $ ( stringify ! ( $ a ) , " = {:?}, " ) ,* ) , $ ( $ a ) ,* ) ; } }

#[allow(dead_code)]
pub const INF: u64 = 1 << 60;

#[derive(Eq, PartialEq, Clone, Debug)]
/// Equivalent to std::cmp::Reverse
pub struct Rev<T>(pub T);
impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}
impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> Ordering {
        other.0.cmp(&self.0)
    }
}

#[allow(dead_code)]
fn main() {
    let n = get!(usize);
    let ws = util::gets::<u64>();

    let mut ans = 0;

    let mut pq = BinaryHeap::new();
    let mut hs = vec![Some(BinaryHeap::<Rev<u64>>::new()); n];
    let mut minima: Vec<Option<(u64, bool, bool)>> = vec![None; n];

    let mut ls = vec![None; n];
    let mut rs = vec![None; n];

    for i in 0..n {
        if i + 1 < n {
            pq.push(Rev((ws[i] + ws[i + 1], i)));
            minima[i] = Some((ws[i] + ws[i + 1], true, true));
        }

        if i > 0 {
            ls[i] = Some(i - 1);
        }

        if i + 1 < n {
            rs[i] = Some(i + 1);
        }
    }
    let mut ws: Vec<Option<u64>> = ws.into_iter().map(Some).collect();

    /*
    debug!(pq);
    debug!(ls);
    debug!(rs);
    */

    while let Some(Rev((w, i))) = pq.pop() {
        if minima[i].map(|t| w != t.0).unwrap_or(true) {
            continue;
        }
        /*
        debug!(w, i);
        debug!(minima[i]);
        debug!(ws[i]);
        debug!(hs[i]);
        debug!(rs[i]);
        */

        let (_, l, r) = minima[i].take().unwrap();

        ans += w;

        let a = if l {
            ws[i].take().unwrap()
        } else {
            hs[i].as_mut().unwrap().pop().unwrap().0
        };

        let b = if r {
            ws[rs[i].unwrap()].take().unwrap()
        } else {
            hs[i].as_mut().unwrap().pop().unwrap().0
        };

        //debug!(w, i);
        debug_assert_eq!(w, a + b);

        hs[i].as_mut().unwrap().push(Rev(w));

        if r {
            let ri = rs[i].take().unwrap();

            minima[ri] = None;
            ls[ri] = None;

            if let Some(rr) = rs[ri].take() {
                ls[rr] = Some(i);
                rs[i] = Some(rr);
            } else {
                rs[i] = None;
            }

            let mut hr = hs[ri].take().unwrap();
            let h = hs[i].as_mut().unwrap();
            h.append(&mut hr);
        }

        let mut i = i;

        if l {
            if let Some(li) = ls[i].take() {
                if let Some(ri) = rs[i].take() {
                    ls[ri] = Some(li);
                    rs[li] = Some(ri);
                } else {
                    rs[li] = None;
                }

                minima[li] = None;

                let mut hr = hs[i].take().unwrap();
                let h = hs[li].as_mut().unwrap();
                h.append(&mut hr);

                i = li;
            }
        }

        let i = i;
        minima[i] = None;

        let mut m = 1 << 60;

        if hs[i].as_ref().unwrap().len() > 1 {
            let a = hs[i].as_mut().unwrap().pop().unwrap().0;
            let b = hs[i].as_mut().unwrap().pop().unwrap().0;

            if m > a + b {
                minima[i] = Some((a + b, false, false));
                m = a + b;
            }

            hs[i].as_mut().unwrap().push(Rev(a));
            hs[i].as_mut().unwrap().push(Rev(b));
        }

        if let (Some(a), Some(b)) = (ws[i], rs[i].and_then(|j| ws[j])) {
            if m > a + b {
                minima[i] = Some((a + b, true, true));
                m = a + b;
            }
        }

        if let (Some(a), Some(&Rev(b))) = (ws[i], hs[i].as_ref().unwrap().peek()) {
            if m > a + b {
                minima[i] = Some((a + b, true, false));
                m = a + b;
            }
        }

        if let (Some(&Rev(a)), Some(b)) =
            (hs[i].as_ref().unwrap().peek(), rs[i].and_then(|j| ws[j]))
        {
            if m > a + b {
                minima[i] = Some((a + b, false, true));
                // m = a + b;
            }
        }

        if let Some((w, _, _)) = minima[i] {
            pq.push(Rev((w, i)));
            // debug!("push", w, i);
        }
    }

    println!("{}", ans);
}



Measured memory_bytes: 5056