Problem:
Score : 700 points

Problem Statement

Snuke loves constructing integer sequences.

There are N piles of stones, numbered 1 through N.
The pile numbered i consists of a_i stones.

Snuke will construct an integer sequence s of length Σa_i, as follows:

Among the piles with the largest number of stones remaining, let x be the index of the pile with the smallest index. Append x to the end of s.

Select a pile with one or more stones remaining, and remove a stone from that pile.

If there is a pile with one or more stones remaining, go back to step 1. Otherwise, terminate the process.

We are interested in the lexicographically smallest sequence that can be constructed. For each of the integers 1,2,3,...,N, how many times does it occur in the lexicographically smallest sequence?

Constraints

1 ≤ N ≤ 10^{5}

1 ≤ a_i ≤ 10^{9}

Input

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

N
a_1 a_2 ... a_{N}

Output

Print N lines. The i-th line should contain the number of the occurrences of the integer i in the lexicographically smallest sequence that can be constructed.

Sample Input 1

2
1 2

Sample Output 1

2
1

The lexicographically smallest sequence is constructed as follows:

Since the pile with the largest number of stones remaining is pile 2, append 2 to the end of s. Then, remove a stone from pile 2.

Since the piles with the largest number of stones remaining are pile 1 and 2, append 1 to the end of s (we take the smallest index). Then, remove a stone from pile 2.

Since the pile with the largest number of stones remaining is pile 1, append 1 to the end of s. Then, remove a stone from pile 1.

The resulting sequence is (2,1,1). In this sequence, 1 occurs twice, and 2 occurs once.

Sample Input 2

10
1 2 1 3 2 4 2 5 8 1

Sample Output 2

10
7
0
4
0
3
0
2
3
0

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

use std::io::*;
use std::str::*;
use std::collections::BinaryHeap;
use std::cmp::Ordering;

use std::cmp;

fn main() {
    // 最も大きいものを貪欲に取っていくのが最適
    solve();
}
fn solve() {
    let n = read::<usize>();
    let mut a = read_vec::<usize>(n);
    let mut memo_max = vec![0; n + 10];
    let mut x = a[0];
    for i in 0..n {
        memo_max[i + 1] = cmp::max(memo_max[i], a[i]);
    }

    let mut heap = BinaryHeap::new();
    let mut cnt = 0;
    let mut ans = vec![0; n];
    //println!("{:?}", memo_max);
    for i in (0..n).rev() {
        if memo_max[i] > a[i] {
            // 使われない
            ans[i] = 0;
            heap.push(Node { val: -(a[i] as i64) });
        } else {
            if memo_max[i + 1] > memo_max[i] {
                ans[i] += (cnt + 1) * (memo_max[i + 1] - memo_max[i]);
                cnt += 1;
            }
            //これよりも右にあるa[i]より大きいもんはmemo_max[i]になる
            while let Some(Node { val }) = heap.pop() {
                let val_plus = -val;
                if val_plus < a[i] as i64 {
                    heap.push(Node { val: val });
                    break;
                } else {
                    cnt += 1;
                    ans[i] += (val_plus as usize - memo_max[i]);
                }
            }
        }
    }
    for i in &ans {
        println!("{}", i);
    }
}

fn solve_() {
    let n = read::<usize>();
    let mut a = read_vec::<usize>(n);
    let sum = a.iter().fold(0, |acc, i| acc + i);
    let mut v = vec![];
    for i in 0..sum {
        let ma = a.iter().fold(0, |acc, i| cmp::max(acc, *i));
        let mut new = n;
        for j in (0..n).rev() {
            if a[j] == ma {
                new = j;
            }
        }
        v.push(new + 1);
        let huga = new;
        for j in (0..n) {
            if a[j] == ma {
                new = j;
            }
        }
        a[new] -= 1;
        println!("{}:{}:{:?}", huga + 1, new + 1, a);
    }
    println!("{:?}", v);
}


#[allow(dead_code)]
fn read_vec<T: FromStr>(n: usize) -> Vec<T> {
    let mut v = vec![];
    for i in 0..n {
        let a = read::<T>();
        v.push(a);
    }
    v
}

#[allow(dead_code)]
#[derive(Copy, Clone, Eq, PartialEq)]
struct Node {
    val: i64,
}
impl Ord for Node {
    fn cmp(&self, other: &Node) -> Ordering {
        other.val.cmp(&self.val)
    }
}
impl PartialOrd for Node {
    fn partial_cmp(&self, other: &Node) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}
#[allow(dead_code)]
fn read_str() -> Vec<char> {
    let s: String = read();
    s.chars().collect::<Vec<char>>()
}
#[allow(dead_code)]
fn read<T: FromStr>() -> T {
    let stdin = stdin();
    let stdin = stdin.lock();
    let s = stdin
        .bytes()
        .map(|c| c.unwrap() as char)
        .skip_while(|c| c.is_whitespace())
        .take_while(|c| !c.is_whitespace())
        .collect::<String>();
    s.parse::<T>().unwrap_or_else(
        |_| panic!("Faild to parse {}", s),
    )
}