use std::io::{read_to_string, stdin};

fn main() {
    let temp = read_to_string(stdin()).unwrap();
    let mut iter = temp
        .split_ascii_whitespace()
        .map(|x| x.parse::<usize>().unwrap());
    loop {
        let n = iter.next().unwrap();
        if n == 0 {
            break;
        }

        let arr = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<_>>();

        let mut stack = vec![(0, 0)];
        let mut res = 0;
        for i in 1..=n {
            let e = arr[i - 1];
            let (mut top_i, mut top_e) = *stack.last().unwrap();

            while top_e >= e {
                let (p_i, p_e) = stack.pop().unwrap();
                if stack.is_empty() {
                    break;
                }
                (top_i, top_e) = *stack.last().unwrap();
                res = res.max((i - top_i - 1) * p_e);
            }
            if stack.is_empty() {
                stack.push((i, e));
                continue;
            }
            stack.push((i, e));
            res = res.max((i - top_i) * e);
        }
        while stack.len() > 1 {
            let (ti, te) = stack.pop().unwrap();
            let (top_i, top_e) = stack.last().unwrap();
            res = res.max((n - top_i) * te);
        }

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