upated cost fuction