Well, thanks to [color=red]kawigi [/color] for so many good problems on Sphere, but this one is purely mathematical, coding part is sorting only. Once we started to programming, let's do programming 
We would like to see such problems as "Exchange Operations" here, and 851 - "Maze" on UVa.