Optimization code with weighting function for the reconstruction of coronal magnetic fields