N = gets.to_i A = gets.split.map(&:to_i) raise if A[0] != 0 c = [ [0, [ [1, [ [4, []], [3, []] ]], [2, []] ]] ] A.each do |i| x, y = c[i] puts x break unless y c = y end