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

def f(a, l, r)
    return false if l >= r
    i = l
    i += 1 until i > r || a[i] != a[l]
    return true if i > r
    return true if i > l + 1 && f(a, i, r)

    j = i
    loop do
        j += 1 until j > r || a[j] == a[l]
        break if j > r
        return true if f(a, i, j - 1) && g(a, j, r)

        j += 1 until j > r || a[j] != a[l]
        break if j > r
    end
    return false
end

def g(a, l, r)
    return true if l >= r
    i = l
    i += 1 until i > r || a[i] != a[l]
    return true if i > r
    return true if f(a, i, r)

    j = i
    loop do
        j += 1 until j > r || a[j] == a[l]
        break if j > r
        return true if f(a, i, j - 1) && g(a, j, r)

        j += 1 until j > r || a[j] != a[l]
        break if j > r
    end
    return false
end

p f(a, 0, n - 1)

