Charge state dynamics and optically detected electron spin resonance contrast of shallow nitrogen-vacancy centers in diamond