n = gets.to_i
a = gets.split.map(&:to_i)

p = 0
r = n-1

x = a[r]
i = p - 1

for j in p .. r - 1
    if a[j] <= x then
        i += 1
        k = a[i]
        a[i] = a[j]
        a[j] = k
    end
end
k = a[i + 1]
a[i + 1] = a[r]
a[r] = k

for k in 0..a.length-1
    if k == i+1 then
        printf("[%d]", a[k])
    else
        printf("%d", a[k])
    end
    if k != a.length-1 then
        printf(" ")
    end
end
printf("\n")
