Solved 591 div2 500 problem