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 C++14 (GCC 5.4.1) (judge verdict: Accepted)

// Copyright (C) 2017 Sayutin Dmitry.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; version 3

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with this program; If not, see <http://www.gnu.org/licenses/>.

#include <iostream>
#include <vector>
#include <stdint.h>
#include <algorithm>
#include <set>
#include <map>
#include <array>
#include <queue>
#include <stack>
#include <functional>
#include <utility>
#include <string>
#include <assert.h>
#include <iterator>
#include <cstdint>
#include <cinttypes>

using std::cin;
using std::cout;
using std::cerr;

using std::vector;
using std::map;
using std::array;
using std::set;
using std::string;

using std::pair;
using std::make_pair;

using std::min;
using std::abs;
using std::max;

using std::unique;
using std::sort;
using std::generate;
using std::reverse;
using std::min_element;
using std::max_element;

#ifdef LOCAL
#define LASSERT(X) assert(X)
#else
#define LASSERT(X) {}
#endif

template <typename T>
T input() {
    T res;
    cin >> res;
    LASSERT(cin);
    return res;
}

template <typename IT>
void input_seq(IT b, IT e) {
    std::generate(b, e, input<typename std::remove_reference<decltype(*b)>::type>);
}

#define SZ(vec)         int((vec).size())
#define ALL(data)       data.begin(),data.end()
#define RALL(data)      data.rbegin(),data.rend()
#define TYPEMAX(type)   std::numeric_limits<type>::max()
#define TYPEMIN(type)   std::numeric_limits<type>::min()


const int maxn = 1e5 + 228;

vector<int> tree[4 * maxn];
vector<int64_t> tree2[4 * maxn];
vector<int> in;

void build(int node, int node_l, int node_r) {
    if (node_l == node_r - 1)
        tree[node] = {in[node_l]};
    else {
        int mid = node_l + (node_r - node_l) / 2;
        build(2 * node + 1, node_l, mid);
        build(2 * node + 2, mid, node_r);

        tree[node].resize(node_r - node_l);
        std::merge(ALL(tree[2 * node + 1]), ALL(tree[2 * node + 2]), tree[node].begin());
    }
    
    tree2[node].resize(node_r - node_l + 1);
    for (int i = 0; i != node_r - node_l; ++i)
        tree2[node][1 + i] = tree2[node][i] + tree[node][i];
}

int64_t count(int node, int node_l, int node_r, int l, int r, int x0, int x1) {
    if (l <= node_l and node_r <= r) {
        int p0 = std::lower_bound(ALL(tree[node]), x0) - tree[node].begin();
        int p1 = std::upper_bound(ALL(tree[node]), x1) - tree[node].begin();
        return (tree2[node][p1] - tree2[node][p0]) - (x0 - 1) * 1ll * (p1 - p0) + (SZ(tree[node]) - p1) * 1ll * (x1 - x0 + 1);
    }
    if (node_r <= l or r <= node_l)
        return 0;
    int mid = node_l + (node_r - node_l) / 2;
    return count(2 * node + 1, node_l, mid, l, r, x0, x1)
         + count(2 * node + 2, mid, node_r, l, r, x0, x1);
}

int main() {
    std::iostream::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    
    // code here.
    in.resize(input<int>());
    input_seq(ALL(in));

    build(0, 0, SZ(in));

    int mx = 0;
    for (int i = 0; i != SZ(in); ++i) {
        int64_t ans = 0;
        if (in[i] > mx) {
            ans = count(0, 0, SZ(in), i, SZ(in), mx + 1, in[i]);
            mx = in[i];
        }
        
        cout << ans << "\n";
    }

    
    return 0;
}