ZCO17002 Optimization Problem

I am getting SIGABRT or SIGSEGV or TLE for this problem. Any idea on how I can improve my solution. I have looked through other people’s solutions but I couldn’t understand them. So please any suggestions.
Problem Link : CodeChef: Practical coding for everyone
My Solution : CodeChef: Practical coding for everyone

You might find this helpful. Also, please search for the editorials of zco/inoi problems (or any problem in that matter) and post your doubts in the comments section below the editorial. For zco/inoi problems you can always ping me for any help.

Thank you very much, I was unaware of the editorials for the ZCO problems.