Codechef question: PHCUL

Question URL: https://www.codechef.com/NOV19B/problems/PHCUL
Cannot think of any solution less than O(mnk)