Grew up in Russia, lived in the States, moved to Germany, sometimes live in Spain. I program since I was 13. I used to program games, maps and now I reverse engineer password managers and other stuff
Location
Berlin and Málaga
Education
MS in CS from State Polytechnic University of St. Petersburg
Have you tested on the maximal
N = 1e6
as stated in the problem description? Your solution seems to beO(N*N)
so it will be really slow.