Title: Partition

Quick sort is based on the Divide-and-conquer approach. In QuickSort(A, p, r), first, a procedure Partition(A, p, r) divides an array A[p..r] into two subarrays A[p..q-1] and A[q+1..r] such that each element of A[p..q-1] is less than or equal to A[q], which is, in turn, less than or equal to each element of A[q+1..r]. It also computes the index q.

In the conquer processes, the two subarrays A[p..q-1] and A[q+1..r] are sorted by recursive calls of QuickSort(A, p, q-1) and QuickSort(A, q+1, r).

Your task is to read a sequence A and perform the Partition based on the following pseudocode:

Partition(A, p, r)
1 x = A[r]
2 i = p-1
3 for j = p to r-1
4     do if A[j] <= x
5        then i = i+1
6            exchange A[i] and A[j] 
7 exchange A[i+1] and A[r]
8 return i+1

Note that, in this algorithm, Partition always selects an element A[r] as a pivot element around which to partition the array A[p..r].

Input:
The first line of the input includes an integer n, the number of elements in the sequence A.
In the second line, Ai (i = 1,2,...,n), elements of the sequence are given separated by space characters.

Output:
Print the sorted sequence. Two contiguous elements of the sequence should be separated by a space character. The element which is selected as the pivot of the partition should be indicated by [ ].

Constraints:
- 1 ≤ n ≤ 100,000
- 0 ≤ Ai ≤ 100,000

Sample Input 1:
12
13 19 9 5 12 8 7 4 21 2 6 11

Sample Output 1:
9 5 8 7 4 2 6 [11] 21 13 19 12

# Buggy version:
n = int(input())
a = list(map(int, input().split()))

s = 0
last = a[-1]

for i in range(n-1):
    if a[i] <= last:
        tmp = a[i]
        a[i] = a[s]
        a[s] = tmp
        s += 1
tmp = a[-1]
a[-1] = a[s]
a[s] = tmp

for i in range(1, s):
    print(a[i], end=' ')
print('[' + str(a[s]) + '] ', end='')
for i in range(s+1, n-1):
    print(a[i], end=' ')
print(a[n-1])


# Fixed version:
n = int(input())
a = list(map(int, input().split()))

s = 0
last = a[-1]

for i in range(n-1):
    if a[i] <= last:
        tmp = a[i]
        a[i] = a[s]
        a[s] = tmp
        s += 1
tmp = a[-1]
a[-1] = a[s]
a[s] = tmp

for i in range(0, s):
    print(a[i], end=' ')
print('[' + str(a[s]) + '] ', end='')
for i in range(s+1, n-1):
    print(a[i], end=' ')
print(a[n-1])

# Buggy version:
def Partition(A,p,r):
    x = A[r]
    i = p-1
    for j in range(p,r):
        if (A[j] <= x):
            i += 1
            A[i],A[j] = A[j],A[i]
            print("swap:",i,j)
    A[i+1],A[r] = A[r],A[i+1]
    print('swap:',i+1,r)
    return i+1

n = int(input())
st = input()
data = list(map(int,st.split()))

mid = Partition(data,0,len(data)-1)
for i in data:
    print(i)
for i in range(0,mid):
    print(data[i],end=" ")
print("[{0}] ".format(data[mid]),end="")
for i in range(mid+1,len(data)-1):
    print(data[i],end=" ")
print(data[len(data)-1])

# Fixed version:
def Partition(A,p,r):
    x = A[r]
    i = p-1
    for j in range(p,r):
        if (A[j] <= x):
            i += 1
            A[i],A[j] = A[j],A[i]
    A[i+1],A[r] = A[r],A[i+1]
    return i+1

n = int(input())
st = input()
data = list(map(int,st.split()))

mid = Partition(data,0,len(data)-1)
for i in range(0,mid):
    print(data[i],end=" ")
print("[{0}] ".format(data[mid]),end="")
for i in range(mid+1,len(data)-1):
    print(data[i],end=" ")
print(data[len(data)-1])

# Buggy version:
def partition(a, p, r):
    x = a[r]
    i = p - 1
    for j in range(p, r):
        if a[j] <= x:
            i = i + 1
            a[i], a[j] = a[j], a[i]
    a[i+1], a[r] = a[r], a[i+1]
    return i+1

n = int(input())
a = list(map(int, input().split()))
m = partition(a, 0, n-1)
a[m] = "[%s]"%(a[m])
print(" ".join(a))

# Fixed version:
def partition(a, p, r):
    x = a[r]
    i = p - 1
    for j in range(p, r):
        if a[j] <= x:
            i = i + 1
            a[i], a[j] = a[j], a[i]
    a[i+1], a[r] = a[r], a[i+1]
    return i+1

n = int(input())
a = list(map(int, input().split()))
m = partition(a, 0, n-1)
a = list(map(str, a))
a[m] = "[%s]"%(a[m])
print(" ".join(a))

# Buggy version:
class Partition:
    def partion(self, a, p, r): 
        x = a[-1]
        i = p - 1
        for j in range(p, r):
            if a[j] <= x:
                i += 1
                a[i], a[j] = a[j], a[i]
            print(a)           
        # a[i + 1], a[-1] = a[-1], a[i + 1]
        print(a)
        return((a, i))

if __name__ == '__main__':
    n = int(input().rstrip())
    a = [int(x) for x in input().rstrip().split(" ")]
    x = Partition()
    a, i = x.partion(a, 0, n)
    res = []
    for j in range(len(a)):
        if j == i:
            res.append("[{}]".format(a[j]))
        else:
            res.append(str(a[j]))
    print(" ".join(res))

# Fixed version:
class Partition:
    def partion(self, a, p, r): 
        x = a[-1]
        i = p - 1
        for j in range(p, r):
            if a[j] <= x:
                i += 1
                a[i], a[j] = a[j], a[i]
        # a[i + 1], a[-1] = a[-1], a[i + 1]
        return((a, i))

if __name__ == '__main__':
    n = int(input().rstrip())
    a = [int(x) for x in input().rstrip().split(" ")]
    x = Partition()
    a, i = x.partion(a, 0, n)
    res = []
    for j in range(len(a)):
        if j == i:
            res.append("[{}]".format(a[j]))
        else:
            res.append(str(a[j]))
    print(" ".join(res))

# Buggy version:
class Partition:
    def partion(self, a, p, r): 
        x = a[-1]
        i = p - 1
        for j in range(p, r):
            if a[j] <= x:
                i += 1
                a[i], a[j] = a[j], a[i]                
        a[i + 1], a[-1] = a[-1], a[i + 1]
        return((a, i))

if __name__ == '__main__':
    n = int(input().rstrip())
    a = [int(x) for x in input().rstrip().split(" ")]
    x = Partition()
    a, i = x.partion(a, 0, n)
    res = []
    for j in range(len(a)):
        if j == i:
            res.append("[{}]".format(a[j]))
        else:
            res.append(str(a[j]))
    print(" ".join(res))

# Fixed version:
class Partition:
    def partion(self, a, p, r): 
        x = a[r]
        i = p - 1
        for j in range(p, r):
            if a[j] <= x:
                i += 1
                a[i], a[j] = a[j], a[i]
        a[i + 1], a[r] = a[r], a[i + 1]
        return(i+1)

if __name__ == '__main__':
    n = int(input().rstrip())
    a = [int(x) for x in input().rstrip().split(" ")]
    x = Partition()
    i = x.partion(a, 0, n-1)
    a = [str(i) for i in a]
    a[i] = "[{}]".format(a[i])
    print(" ".join(a))

# Buggy version:
def partition(a, p, r):
    x = a[r]
    i = p - 1
    for j in range(p, r):
        if a[j] <= x:
            i = i + 1
            a[i], a[j] = a[j], a[i]
    a[i+1], a[r] = a[r], a[i+1]
    print(*a)
    return i+1

n = int(input())
a = list(map(int, input().split()))
partition(a, 0, n)

# Fixed version:
def partition(p, r):
    x = a[r]
    i = p-1

    for j in range(p, r):
        if a[j] <= x:
            i += 1
            a[i], a[j] = a[j], a[i]

    a[i+1], a[r] = a[r], a[i+1]
    return i+1


n = int(input())
a = list(map(int, input().split()))
idx = partition(0, n-1)
a[idx] = [a[idx]]
print(*a)

# Buggy version:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_6_B&lang=jp
#partition

def partition(target_list):
    x = target_list[-1]
    i = -1

    for j in range(len(target_list))[:-1]:
        if target_list[j] <= x:
            i = i+1
            tmp = target_list[i]
            target_list[i] = target_list[j]
            target_list[j] = tmp
    tmp = target_list[-1]
    target_list[-1] = target_list[i + 1]
    target_list[i + 1] = tmp
    
    return  i + 1

def convert(l):
    return " ".join([str(n) for n in l])

if __name__ == "__main__":
    n_list = int(input())
    target_list = [int(n) for n in input().split()]

    center_index = partition(target_list)
    print(convert(target_list[:center_index]) + " ["+ str(target_list[center_index]) + "] " + convert(target_list[center_index:]))

# Fixed version:
#http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_6_B&lang=jp
#partition

def partition(target_list):
    x = target_list[-1]
    i = -1

    for j in range(len(target_list))[:-1]:
        if target_list[j] <= x:
            i = i+1
            tmp = target_list[i]
            target_list[i] = target_list[j]
            target_list[j] = tmp
    tmp = target_list[-1]
    target_list[-1] = target_list[i + 1]
    target_list[i + 1] = tmp
    
    return  i + 1

def convert(l):
    return " ".join([str(n) for n in l])

if __name__ == "__main__":
    n_list = int(input())
    target_list = [int(n) for n in input().split()]

    center_index = partition(target_list)
    print(convert(target_list[:center_index]) + " ["+ str(target_list[center_index]) + "] " + convert(target_list[center_index + 1:]))

# Buggy version:
def partition(A, p, r):
    """
    ???????????????????????????????????????A[r]?????°?????????????????????????????????????????????A[r-1]??¨?´??????????????????§??¨???
    """
    x = A[r]p
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            temp = A[i]
            A[i] = A[j]
            A[j] = temp
    temp = A[i+1]
    A[i+1] = A[r]
    A[r] = temp
    return i+1


if __name__ == '__main__':
    # ??????????????\???
    # A = [13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11]
    num_of_data = int(input())
    A = [int(x) for x in input().split(' ')]

    # ???????????????
    p = partition(A, 0, len(A)-1)  # ????????????????????????????????????len(A)??§?????????-1???????????????????????????

    # ???????????¨???
    left = A[:p]
    partition = A[p]
    right = A[p+1:]
    print('{0} [{1}] {2}'.format(' '.join(map(str, left)), partition, ' '.join(map(str, right))))

# Fixed version:
def partition(A, p, r):
    """
    ???????????????????????????????????????A[r]?????°?????????????????????????????????????????????A[r-1]??¨?´??????????????????§??¨???
    """
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            temp = A[i]
            A[i] = A[j]
            A[j] = temp
    temp = A[i+1]
    A[i+1] = A[r]
    A[r] = temp
    return i+1


if __name__ == '__main__':
    # ??????????????\???
    # A = [13, 19, 9, 5, 12, 8, 7, 4, 21, 2, 6, 11]
    num_of_data = int(input())
    A = [int(x) for x in input().split(' ')]

    # ???????????????
    p = partition(A, 0, len(A)-1)  # ????????????????????????????????????len(A)??§?????????-1???????????????????????????

    # ???????????¨???
    left = A[:p]
    partition = A[p]
    right = A[p+1:]
    print('{0} [{1}] {2}'.format(' '.join(map(str, left)), partition, ' '.join(map(str, right))))

# Buggy version:
# -*- coding: utf-8 -*-


def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i]
    return i + 1


def main():
    n = int(input())
    A = [int(e) for e in input().split()]
    p = 0
    idx = partition(A, p, n - 1)
    A = [str(e) for e in A]
    A[idx] = "[{}]".format(A[idx])
    return " ".join(A)


print(main())

# Fixed version:
# -*- coding: utf-8 -*-


def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1


def main():
    n = int(input())
    A = [int(e) for e in input().split()]
    p = 0
    idx = partition(A, p, n - 1)
    A = [str(e) for e in A]
    A[idx] = "[{}]".format(A[idx])
    return " ".join(A)


print(main())

# Buggy version:
def partition(A,p=0, r=None):
    if r is None:
        r = len(A)-1
    x = A[r]
    i = p-1
    for j in range(p,r):
        while A[j] <= x:
            i += 1
            swap(A,i,j)
    swap(A,i+1,r)
    return i+1

if __name__ == "__main__:
    input()
    A = list(map(int, input().strip().split()))
    k = partition(A)
    B = list(map(str, A))
    B[k] = "[{}]".format(B[k])
    print(" ".join(B))

# Fixed version:
def swap(A,i,j):
    A[i],A[j] = A[j],A[i]
    return A

def partition(A,p=0, r=None):
    if r is None:
        r = len(A)-1
    x = A[r]
    i = p-1
    for j in range(p,r):
        if A[j] <= x:
            i += 1
            swap(A,i,j)
    swap(A,i+1,r)
    return i+1

if __name__ == "__main__":
    input()
    A = list(map(int, input().strip().split()))
    k = partition(A)
    B = list(map(str, A))
    B[k] = "[{}]".format(B[k])
    print(" ".join(B))

# Buggy version:
n = int(input())
a = list(map(int,input().split()))

x = a[-1]
i = -1

for j in range(len(a)-1):
    if a[j] <= x:
        i += 1
        a[i], a[j] = a[j], a[i]

a[i + 1], a[n] = "[" + str(a[n]) + "]", a[i + 1]

print(*a)

# Fixed version:
n = int(input())
a = list(map(int,input().split()))

x = a[-1]
i = -1

for j in range(len(a)-1):
    if a[j] <= x:
        i += 1
        a[i], a[j] = a[j], a[i]

a[i + 1], a[n - 1] = "[" + str(a[n - 1]) + "]", a[i + 1]

print(*a)

# Buggy version:
def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i = i + 1
            temp = A[i]
            A[i] = A[j]
            A[j] = temp
    temp = A[i+1]
    A[i+1] = A[r]
    A[r] = temp
    return i+1


if __name__ == "__main__":
    n = int(input())
    B = list(map(int, input().split()))
    p = partition(B, 0, n-1)
    print(B)
    print(" ".join(map(str, B[:p])) + " [" + str(B[p]) + "] " + " ".join(map(str, B[p+1:])))

# Fixed version:

def partition(A,p,r):
    x = A[r]
    i = p-1
    for j in range(p,r):
        if A[j] <= x:
            i = i+1
            (A[i],A[j]) = (A[j],A[i])
    (A[i+1],A[r]) = (A[r],A[i+1])
    return i+1



if __name__ == '__main__':
    n = (int)(input())
    A = list(map(int,input().split()))

    i = partition(A,0,n-1)
    A[i] = "["+str(A[i])+"]"
    print(*A)

# Buggy version:
n=int(input())-1
A=list(map(int,input().split()))
i=0
for j in range(n):
 if A[j]<=A[-1]:A[i],A[j]=A[j],A[i];i+=1
A[i],A[n]=A[n],A[i]
print(" ".join(map(str,A[:k]))+" ["+str(A[i])+"] "+" ".join(map(str,A[k+1:])))


# Fixed version:
n=int(input())-1
A=list(map(int,input().split()))
i=0
for j in range(n):
 if A[j]<=A[-1]:A[i],A[j]=A[j],A[i];i+=1
A[i],A[n]=A[n],A[i]
print(" ".join(map(str,A[:i]))+" ["+str(A[i])+"] "+" ".join(map(str,A[i+1:])))

# Buggy version:
def s():
 n=int(input())-1
 A=list(map(int,input().split()))
 i=0
 for j in range(n):A[j]<=A[-1]and A[i],A[j]=A[j],A[i];i+=1
 A[i],A[n]=A[n],A[i]
 print(" ".join(map(str,A[:i]))+" ["+str(A[i])+"] "+" ".join(map(str,A[i+1:])))
if'__main__'==__name__:s()


# Fixed version:
def s():
 n=int(input())-1
 A=list(map(int,input().split()))
 i=0
 for j in range(n):
  if A[j]<=A[-1]:A[i],A[j]=A[j],A[i];i+=1
 A[i],A[n]=A[n],A[i]
 print(" ".join(map(str,A[:i]))+" ["+str(A[i])+"] "+" ".join(map(str,A[i+1:])))
if'__main__'==__name__:s()

# Buggy version:
def partition(array, p, r):
    x = array[r]
    i = p-1
    for j in range(p, r):
        if array[j] <= x:
            i += 1
            array[i], array[j] = array[j], array[i]
    array[i+1], array[r] = array[r], array[i+1]
    a1 = array[:i+1]
    a2 = "[" + str(array[i+1]) +  "]"
    a3 = array[i+1:]
    array = a1[:]
    array.append(a2)
    array += a3
    print(*array)
    return i+1

n = int(input())
array = [int(a) for a in input().split()]
p = partition(array, 0, n-1)


# Fixed version:
def partition(array, p, r):
    x = array[r]
    i = p-1
    for j in range(p, r):
        if array[j] <= x:
            i += 1
            array[i], array[j] = array[j], array[i]
    array[i+1], array[r] = array[r], array[i+1]
    a1 = array[:i+1]
    a2 = "[" + str(array[i+1]) +  "]"
    a3 = array[i+2:]
    array = a1[:]
    array.append(a2)
    array += a3
    print(*array)
    return i+1

n = int(input())
array = [int(a) for a in input().split()]
p = partition(array, 0, n-1)

# Buggy version:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def patition(a, p, r):
    x = a[r]
    i = p-1
    for j in range(p, r):
        if a[j] <= x:
            i += 1
            a[i], a[j] = a[j], a[i]
    a[i+1], a[r] = a[r], a[i+1]
    return i+1


n = int(stdin.readline())
data = [int(s) for s in stdin.readline().split()]
print(data[-1])
pat = patition(data, 0, len(data)-1)
print(*data[:pat], end=' ')
print('[{}]'.format(data[pat]), *data[pat+1:])

# Fixed version:
#!/usr/bin/env python
from __future__ import division, print_function
from sys import stdin


def patition(a, p, r):
    x = a[r]
    i = p-1
    for j in range(p, r):
        if a[j] <= x:
            i += 1
            a[i], a[j] = a[j], a[i]
    a[i+1], a[r] = a[r], a[i+1]
    return i+1


n = int(stdin.readline())
data = [int(s) for s in stdin.readline().split()]
pat = patition(data, 0, len(data)-1)
print(*data[:pat], end=' ')
print('[{}]'.format(data[pat]), *data[pat+1:])

# Buggy version:


def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1


n = int(input())
A = list(map(int, input().split()))

q =partition(A, 1, len(A) - 1)

print(*A[:q] + " [{0}] " + *A[q + 1:])

# Fixed version:
def partition(A, p, r):
    x = A[r-1]
    i = p-1
    for j in range(p,r):
        if A[j] <= x:
            i = i+1
            A[i], A[j] = A[j], A[i]
    return i+1

n = int(input())
A = list(map(int,input().split()))
num = partition(A, 0, n)
A[num-1] = f"[{A[num-1]}]"
print(*A)

# Buggy version:
def partition(A, p, r):
  x = A[r]
  i = p - 1
  
  for j in range(p, r):
    if A[j] <= x:
      i += 1
      A[i], A[j] = A[j], A[i]
  
  A[i + 1], A[r] = A[r], A[i + 1]
  
  return i + 1
A = [int(num) for num in input().split()][1:]
p = 0
r = len(A)-1
q = partition(A, p, r)
A_str = [str(a) for a in A]
A_str[q] = "[" + A_str[q] + "]"
print(" ".join(A_str))


# Fixed version:
def partition(A, p, r):
    x = A[r]
    i = p - 1
    
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    
    A[i + 1], A[r] = A[r], A[i + 1]
    
    return i + 1
_ = input()
A = [int(num) for num in input().split()]
p = 0
r = len(A)-1
q = partition(A, p, r)
A_str = [str(a) for a in A]
A_str[q] = "[" + A_str[q] + "]"
print(" ".join(A_str))

# Buggy version:
n = int(input())
A = list(map(int, input().split()))

def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x :
            i += 1
            A[i], A[j] = A[j], A[i]

    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1

b = partition(A, 0, n-1)
A[b] = '[{0}]'.format(b)
print(' '.join(map(str, A)))


# Fixed version:
n = int(input())
A = [int(x) for x in input().split()]


def partition(A, p, r):
    x = A[r]
    i = p
    for j in range(p, r):
        if A[j] <= x:
            A[i], A[j] = A[j], A[i]
            i += 1

    A[i], A[r] = A[r], A[i]

    return i


r = partition(A, 0, n - 1)

ans = [str(x) for x in A]
ans[r] = '[{}]'.format(ans[r])
print(' '.join(ans))

# Buggy version:
def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    return i+1

n = int(input())
A = list(map(int, input().split()))
i = partition(A, 0, n)
A[i] = "["+str(A[i])+"]"
print(*A)

# Fixed version:
def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p, r):
        if A[j] <= x:
            i+=1
            A[i],A[j]=A[j],A[i]
    
    A[i+1],A[r]=A[r],A[i+1]
    return i+1

#input
n=int(input())
data=list(map(int, input().split()))
q=partition(data,0,n-1)
data[q]="["+str(data[q])+"]"
print(*data)

# Buggy version:
n = int(input())
A = list(map(int, input().split()))

def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1

i = partition(A, 0, n-1)
print(' '.join(map(str, A[:i])) + ' [' + str(A[i]) + '] ' + ' '.join(map(str, A[i:])))

# Fixed version:
def partition(A, p, r):
  x = A[r]
  i = p - 1
  for j in range (p, r):
    if A[j]  <= x:
      i += 1
      A[i], A[j] = A[j], A[i]
  A[i+1], A[r] = A[r],A[i + 1]
  return i + 1

n = int(input())
a = list(map(int, input().split()))
i = partition(a, 0, n-1)
a[i] = "[" + str(a[i]) + "]"
print(" ".join(map(str, a)))

# Buggy version:


def partition(A, p, r):
    x = A[r]
    i = p - 1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i + 1], A[r] = A[r], A[i + 1]
    return i + 1


n = int(input())
A = list(map(int, input().split()))

q =partition(A, 1, len(A) - 1)

print(*A[:q], end = ' ')
print('[{0}]'.format(A[q]), end = ' ')
print(*A[q + 1:])

# Fixed version:
def partition(A, p, r):
    x = A[r]
    i = p-1
    for j in range(p, r):
        if A[j] <= x:
            i += 1
            A[i], A[j] = A[j], A[i]
    A[i+1], A[r] = A[r], A[i+1]
    A[i+1] = "[{}]".format(A[i+1])


n = int(input())
A = list(map(int, input().split()))
partition(A, 0, n-1)
print(*A)