Optimization of Dynamical Systems with Impulse Controls and Shocks