class Xxx attr_accessor :answer def initialize @answer = 0 end def set_inputs @n = gets.chomp.to_i @block_lens = gets.chomp.split(" ").map{|n| n.to_i} end def execute sorting = [] keeping = [] prev = nil @block_lens.each do |n| if prev.nil? prev = n keeping << n next end if prev > n sorting << n else keeping << n prev = n end end @answer = sorting.size # まだ並んでないなら再度並べる if keeping[0] < sorting.max sorting2 = [] prev2 = nil keeping.each do |n| if prev2.nil? prev2 = n next end if prev2 > n sorting2 << n else prev2 = n end end end @answer += sorting2.size end end if $0 == __FILE__ ins = Xxx.new ins.set_inputs ins.execute puts ins.answer end