def calc(idx, total) end def main # 2 <= n <= 100 n = gets.to_i # 0..n-1 ws = gets.split.map(&:to_i) total = ws.inject(:+) return 'impossible' if total.odd? half = total / 2 dp = Array.new(n) { Array.new(half + 1, 0) } q = [{ idx: -1, total: 0 }] res = 'impossible' while !q.empty? cur = q.shift if cur[:total] == half res = 'possible' break end next if cur[:idx] == n - 1 if cur[:total] + ws[cur[:idx] + 1] <= half if dp[cur[:idx] + 1][cur[:total] + ws[cur[:idx] + 1]].zero? q << { idx: cur[:idx] + 1, total: cur[:total] + ws[cur[:idx] + 1] } dp[cur[:idx] + 1][cur[:total] + ws[cur[:idx] + 1]] = 1 end end if dp[cur[:idx] + 1][cur[:total]].zero? q << { idx: cur[:idx] + 1, total: cur[:total] } dp[cur[:idx] + 1][cur[:total]] = 1 end end res end puts main