A maximum principle in spectral optimization problems for elliptic operators subject to mass density perturbations