eval'N,*A='+$<.read.split*?, b=A.max<=>A[-1];$><<[*A.sort![0..-3],*A[-2,2].rotate(b)]