AC solution maximal gap 20ms

O(nlogn) version takes 28ms