INOI 2020 Department Strength TLE help

Here is my submission to the problem:
https://www.codechef.com/viewsolution/58627626

I tried to optimize my code as much as possible but still got a TLE in subtask 2.