Use sum function to construct objective function.