def 🎍(a,b,c) a != c && ((a < b && b > c) || (a > b && b < c)) end line1 = gets.chomp! line2 = gets.chomp! abort if gets n = line1.to_i arr = line2.split.map(&:to_i) abort 'a' if arr.find{|a| 1 > a || a > n} abort '🎍' if arr.each_cons(3).all?{|a| 🎍(*a)} # - - - - - not_kado_idxs = arr.each_cons(3).each_with_index.map{|a,i| 🎍(*a) ? nil : i}.compact if not_kado_idxs.size > 20 puts 'No' exit end validate_idxs = not_kado_idxs.map{|i| (-1..0).map{|e|i+e}.to_a}.flatten.uniq.select{|i| 0 <= i && i < n-2} ok = false n.times do |i| not_kado_idxs.each do |ji| 3.times do |jj| j = ji+jj next if i == j arr[i], arr[j] = arr[j], arr[i] ok ||= validate_idxs.each.all?{|x| 🎍(arr[x], arr[x+1], arr[x+2])} && (-2..0).map{|e|i+e}.select{|i| 0 <= i && i < n-2}.all?{|x| 🎍(arr[x], arr[x+1], arr[x+2])} arr[i], arr[j] = arr[j], arr[i] end end end puts ok ? 'Yes' : 'No'