# frozen_string_literal: true N = gets.chomp.chars.map(&:to_i) result = N (0..N.size - 2).each do |index| rights = index + 1..N.size - 1 right_max = N[rights].max next unless N[index] < right_max rights.to_a.reverse.each do |right_index| next unless N[right_index] == right_max result[index], result[right_index] = result[right_index], result[index] puts result.join exit end end puts result.join