here is a challenging problem in its own : link is below http://mirror.codeforces.com/blog/entry/4611
i have tried different approaches for this one , but none produces answers for 10 testcases within 1 minute time .
hope somebody is able to crack it .