Convex Minimization on a Grid and Applications