Problem:
Score : 400 points

Problem Statement

One day, Snuke was given a permutation of length N, a_1, a_2, ..., a_N, from his friend.

Find the following:

Constraints

1 ≦ N ≦ 200,000

(a_1, a_2, ..., a_N) is a permutation of (1, 2, ..., N).

Input

The input is given from Standard Input in the following format:

N
a_1 a_2 ... a_N

Output

Print the answer.

Note that the answer may not fit into a 32-bit integer.

Sample Input 1

3
2 1 3

Sample Output 1

9

Sample Input 2

4
1 3 2 4

Sample Output 2

19

Sample Input 3

8
5 4 8 1 2 6 7 3

Sample Output 3

85

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

#[allow(unused_imports)]
use std::io::{self, BufRead};
#[allow(unused_imports)]
use std::cmp::{min, max};

#[allow(dead_code)]
fn readln_to_vec<T>(buf: &mut BufRead) -> Vec<T> 
where T: std::str::FromStr,
      <T as std::str::FromStr>::Err: std::fmt::Debug
{
    let mut s = String::new();
    buf.read_line(&mut s).unwrap();
    s.split(" ")
        .map(|x| x.trim())
        .filter(|x| !x.is_empty())
        .filter_map(|x| x.parse().ok())
        .collect()
}

#[allow(dead_code)]
fn readln_to<T>(buf: &mut BufRead) -> T
where T: std::str::FromStr + std::clone::Clone,
      <T as std::str::FromStr>::Err: std::fmt::Debug
{
    readln_to_vec::<T>(buf)[0].clone()
}

#[allow(unused_macros)]
macro_rules! readln_to_tuple {
    (
        $bufRead: expr, ( $( $t: ty ),* )
    ) => (
        {
            let buf: &mut BufRead = $bufRead;
            let ln = readln_to_vec::<String>(buf);
            let mut i = ln.into_iter();
            ( $( i.next().unwrap().parse::<$t>().unwrap() ),* )
        }
        )
}


struct Segtree {
    l: usize,
    r: usize,
    lc: Option<Box<Segtree>>,
    rc: Option<Box<Segtree>>,
    v: usize,
}


impl Segtree {
    fn new(a: &Vec<i64>, l: usize, r: usize) -> Segtree {
        if l+1 == r {
            Segtree{l: l, r: r, lc: None, rc: None, v: l}
        } else {
            let lc = Box::new(Segtree::new(a, l, (l+r)/2));
            let rc = Box::new(Segtree::new(a, (l+r)/2, r));
            let v = if a[lc.v] < a[rc.v] {lc.v} else {rc.v};
            Segtree {
                l: l, r: r,
                lc: Some(lc),
                rc: Some(rc),
                v: v
            }
        }
    }
    fn rmq(&self, a: &Vec<i64>, l: usize, r: usize) -> usize {
        if l == self.l && r == self.r {
            self.v
        } else {
            match (&self.lc, &self.rc) {
                (&Some(ref lc), &Some(ref rc)) => {
                    if r <= lc.r {
                        lc.rmq(a, l, r)
                    } else if rc.l <= l {
                        rc.rmq(a, l, r)
                    } else {
                        let x = lc.rmq(a, l, lc.r);
                        let y = rc.rmq(a, rc.l, r);
                        if a[x] < a[y] {x} else {y}
                    }
                },
                _ => panic!("hoge")
            }
        }
    }
}

fn calc(a: &Vec<i64>, st: &Segtree, l: i64, r: i64) -> i64 {
    if r <= l {
        return 0;
    }

    if l+1 == r {
        return a[l as usize];
    }
    
    let min_idx = st.rmq(a, l as usize, r as usize) as i64;
    calc(a, st, l, min_idx) + calc(a, st, min_idx + 1, r)
        + a[min_idx as usize] * ((r - l + 1) * (r - l) / 2 - (min_idx - l + 1) * (min_idx - l) / 2 - ((r - (min_idx+1) + 1) * (r - (min_idx+1))/2))

}

fn main() {
    let stdin = io::stdin();
    let mut stdin = stdin.lock();
    let stdin = &mut stdin;

    let n: usize = readln_to(stdin);
    let a: Vec<i64> = readln_to_vec(stdin);

    let st = Segtree::new(&a, 0, n);

    println!("{}", calc(&a, &st, 0, n as i64));
}