Fix cost = 31 overflow bug