num = gets.chop
line = gets
num_array = line.split(" ")

class Test 
    def partition(a, pp,r)
        x = a[r].to_i
        i = pp - 1
        j = pp
        while true do
            if j == r then
                break
            end
            if a[j].to_i <= x then
                i += 1
                buf = a[i]
                a[i] = a[j]
                a[j] = buf
            end
            j += 1
        end
        buf2 = a[i + 1]
        a[i + 1] = a[r]
        a[r] = buf2
        # result = ""
        for a_val in a do
            if a_val == a[i + 1] then
                if a_val == a[a.length - 1] then
                    print "["
                    print a_val.to_i 
                    p "]"
                else
                    print "["
                    print a_val.to_i 
                    print "] "
                end
            else
                if a_val == a[a.length - 1] then
                    p a_val.to_i
                else
                    print a_val.to_i
                    print " "
                end
            end
        end
        return i + 1
    end
end
test = Test.new
test.partition(num_array,0,num_array.length - 1)