ex4 - finished cost function