Returning cost for optimize