a = gets.strip.split('').map(&:to_i) #p a score = 0 @max_score = 0 def dfs(a, score) (0...a.length).to_a.combination(3) do |set| #p set #p a subscore = 0 if a[set[0]] != a[set[1]] && a[set[1]] == a[set[2]] #print "#{a[set[0]]}#{a[set[1]]}#{a[set[2]]}\n" subscore = (a[set[0]].to_s + a[set[1]].to_s + a[set[2]].to_s).to_i #p score + subscore if score + subscore > @max_score @max_score = score + subscore end next_a = a.clone #p set set.each do |index| #p next_a #print "index #{index}\n" next_a[index] = nil end next_a.compact! #p next_a dfs(next_a, score + subscore) end end end dfs(a, 0) p @max_score