s = gets.chomp rl = [] [*1..9].reverse.each do |i| [*0..9].reverse.each do |j| next if i == j rl << Regexp.new("(#{i}).*(#{j}).*#{j}") end end ans = 0 while true do z = false rl.each do |i| if s =~ i z = true _s = $& _b = $` _a = $' v1 = $1 v2 = $2 ans += v1.to_i * 100 + v2.to_i * 11 _s.gsub!(v2, "") _s.sub!(v1, "") s = _b + _s + _a break; end end break unless z end puts ans