PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000202. Cắt tóc và số nghịch thế (Haircut)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Cắt tóc và số nghịch thế (Haircut)

Haircut

Source: USACO

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn hair strands in a row, with initial length aia_i micrometers for strand ii. For any integer threshold tt, every strand longer than tt is cut to exactly tt; all other strands remain unchanged. Thus bi=min⁡(ai,t)b_i=\min(a_i,t).

The badness is the number of pairs i<ji<j satisfying bi>bjb_i>b_j, known as inversions. For each threshold t=0,1,…,n−1t=0,1,\ldots,n-1, find the badness of the resulting array. Every threshold is applied to the original lengths independently.

Input

  • The first line contains nn, the number of hair strands.
  • The second line contains nn integers a1,…,ana_1,\ldots,a_n, listed from left to right.

Output

Print exactly nn lines. Line t+1t+1 contains the number of inversions after applying threshold tt, for t=0,1,…,n−1t=0,1,\ldots,n-1.

Subtasks

  • Subtask 1 (20 points): 1≤n≤1001\le n\le100; 0≤ai≤n0\le a_i\le n for every ii.

  • Subtask 2 (30 points): 1≤n≤50001\le n\le5000; 0≤ai≤n0\le a_i\le n for every ii.

  • Subtask 3 (50 points): 1≤n≤1051\le n\le10^5; 0≤ai≤n0\le a_i\le n for every ii.

Examples

Input

5
5 2 3 3 0

Output

0
4
4
5
7

Explanation

For t=0t=0, all values become zero and there are no inversions. For t=1,2t=1,2, the arrays become [1,1,1,1,0][1,1,1,1,0] and [2,2,2,2,0][2,2,2,2,0], each having four inversions ending at the fifth element. For t=3t=3, the array is [3,2,3,3,0][3,2,3,3,0], adding (1,2)(1,2) to those four inversions, for a total of five. At t=4t=4, the array becomes [4,2,3,3,0][4,2,3,3,0] with four inversions ending at 00 plus three starting at the first value, giving seven.