Spectral optimization problems for potentials and measures