Why charges go to the surface: A generalized Thomson problem