you can see my solution, I also used vector of pairs ,my code is in c++. https://www.codechef.com/viewsolution/47658167