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

#GD0000210. Tối thiểu hóa hoán vị (Minimize the Permutation)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tối thiểu hóa hoán vị (Minimize the Permutation)

Minimize the Permutation

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

You are given a permutation p1,p2,…,pnp_1,p_2,\ldots,p_n of the integers 11 through nn. For each ii from 11 to n−1n-1, operation ii swaps the elements currently at adjacent positions ii and i+1i+1.

Operations may be performed in any order, but each operation ii may be used at most once. Performing no operations is also allowed.

Find the lexicographically smallest permutation obtainable. Compare permutations from left to right; the smaller element at the first differing position determines the smaller permutation.

Input

  • The first line contains an integer nn, the permutation length.
  • The second line contains exactly nn integers p1,p2,…,pnp_1,p_2,\ldots,p_n in their initial order.

Output

Print one line of exactly nn space-separated integers: the lexicographically smallest permutation attainable when every adjacent swap position can be used at most once.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le8; pp is a permutation of {1,2,…,n}\{1,2,\ldots,n\} with distinct values.
  • Subtask 2 (30 points): 1≤n≤301\le n\le30; pp is a permutation of {1,2,…,n}\{1,2,\ldots,n\} with distinct values.
  • Subtask 3 (50 points): 1≤n≤1001\le n\le100; pp is a permutation of {1,2,…,n}\{1,2,\ldots,n\} with distinct values.

Examples

Input

5
5 4 1 3 2

Output

1 5 2 4 3

Explanation

From 5 4 1 3 2, use swap position 2 to obtain 5 1 4 3 2; position 4 gives 5 1 4 2 3; position 3 gives 5 1 2 4 3; and position 1 gives 1 5 2 4 3. Every swap position is used once. This is the lexicographically minimum attainable permutation.