def lscan; gets.split.map(&:to_i); end la = '0' total = 0 tw = false dc = false gets.chomp.reverse.chars.each do |c| if c == '0' total += tw ? (dc ? 1 : 2) : 1 if la == '1' dc = false if la == '0' dc = true if la == '1' la = '0' elsif c == '1' tw = la == '1' la = '1' end end total += tw ? (dc ? 1 : 2) : 1 p total