Made SRM 585 DIV 2 solution more efficient.It passes DIV 1 test cases now.