def solve(m) p = Array.new(m) { gets.not_nil!.to_i } res = [] of Tuple(Int32, Array(Int32)) (0...(1 << m)).each do |bit| cnt = 0 v = [] of Int32 m.times do |i| if (bit >> i) & 1 == 1 cnt += p[i] v << i end end res << {cnt, v} end res end n, s = gets.not_nil!.split.map(&.to_i) p1 = solve(n // 2) p2 = solve(n - n // 2) p2.sort_by! { |(sum, _)| sum } ans = [] of Array(Int32) p1.each do |s1, v1| target = s - s1 # Find all elements in p2 with the target sum p2.each do |s2, v2| if s2 == target res = v1.dup v2.each { |x| res << x + n // 2 } ans << res end end end ans.sort.each do |v| puts v.map { |x| x + 1 }.join(" ") end