f=->(d,v){ return 0 if d<0 if v.map{|x|x[d]}.uniq[1] 2**d+[f[d-1,v.select{|x|x[d]>0}],f[d-1,v.select{|x|x[d]<1}]].min else f[d-1,v] end } gets p f[30,gets.split.map(&:to_i)]