n = gets.not_nil!.to_i w = gets.not_nil!.split.map(&.to_i) # if w.size != n # puts "Error: Expected #{n} numbers but got #{w.size}" # exit # end t = w.sum if t.odd? puts "impossible" exit end th = t // 2 # Create a 2D array with n+1 rows and th+1 columns, initialized to 0 m = Array(Array(Int32)).new(n + 1) { Array(Int32).new(th + 1, 0) } (1...n).each do |i| (0..th).each do |j| if j < w[i] m[i][j] = m[i - 1][j] else m[i][j] = Math.max(m[i - 1][j - w[i]] + w[i], m[i - 1][j]) end if m[i][j] == th puts "possible" exit end end end puts "impossible"