Partially solved srm 599 div2 950 pt. problem. Gotta memoize it later on