17
vim, 62 59 54
qrma50%mb:norm@q<cr>ggqOjdd'apjma'b@q<esc>0"qDJ<C-a>D@"i@r<esc>xxdd@"
Wow. This is possibly the hackiest thing I've written for PPCG, and that's saying something.
Input is taken as N on the first line followed by the elements of the array, each on its own line.
qr first...