Efficient Java Solution for spoj ACMCEG2B problem

Problem Statement: https://www.spoj.com/problems/ACMCEG2B/

My Code: https://ide.codingblocks.com/s/75755

With the code I have written, I’m facing Time Limit Exceeded problem.

Can someone please help me by telling an optimized approach in Java?