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

#DSU0000088. Sắp xếp đồng xu (Sorting the Coins)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Sắp xếp đồng xu (Sorting the Coins)

Sorting the Coins

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

There are nn coins in a row, each of type OO (out of circulation) or XX (in circulation). A correctly sorted row contains all OO coins before all XX coins.

In one scan, inspect adjacent pairs from left to right. Whenever an XOXO pair is found, swap it to OXOX and continue scanning. Repeat scans until a scan has no swaps. The sorting hardness is the total number of scans, including the final scan with no swaps.

Initially all coins are OO. At step ii, the coin at position pip_i is replaced with type XX; all positions pip_i are distinct. Find the sorting hardness initially and after every replacement. The scans are hypothetical and do not modify the actual row between replacement steps.

Input

The first line contains nn. The second line is the permutation p1,p2,…,pnp_1,p_2,\ldots,p_n.

Output

Print n+1n+1 integers: the sorting hardness initially and after each replacement.

Subtasks

  • Subtask 1 (20 points): 1≤n≤251\le n\le 25; (p1,…,pn)(p_1,\ldots,p_n) là hoán vị của 1,…,n1,\ldots,n.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; (p1,…,pn)(p_1,\ldots,p_n) là hoán vị của 1,…,n1,\ldots,n.
  • Subtask 3 (50 points): 1≤n≤3⋅1051\le n\le 3\cdot10^5; (p1,…,pn)(p_1,\ldots,p_n) là hoán vị của 1,…,n1,\ldots,n.

Example

Input

4
1 3 4 2

Output

1 2 3 2 1

Explanation

Initially all coins are OO, so one scan suffices. After replacing positions 1,3,4,21,3,4,2 in order, the hardness values are 2,3,2,12,3,2,1.