Solution (source code)

= Solution

For the uniform-gravity <Hamiltonian>, take the three integrals $p_x,p_y,H$. Since the potential does not depend on $x,y$,
$$
\{p_x,p_y\}=0,\qquad\{p_x,H\}=-\partial_xH=0,\qquad\{p_y,H\}=0.
$$
For $g\ne0$, the $mg\,dz$ term in $dH$ makes these three differentials independent. Thus the system is <Liouville integrable>, as the explicit separated solution already demonstrates.

Replacing $mgz$ by a sufficiently smooth arbitrary function $f(z)$ preserves the same commuting integrals. They are independent wherever $p_z$ and $f'(z)$ do not vanish simultaneously. The vertical motion reduces to
$$
\boxed{\frac{p_z^2}{2m}+f(z)=E_z,\qquad
 t-t_0=\pm\sqrt{\frac m2}\int_{z_0}^{z}\frac{d\zeta}{\sqrt{E_z-f(\zeta)}}.}
$$
The horizontal motion remains uniform. Thus an elementary antiderivative is unnecessary: the system is still integrable by a one-dimensional quadrature on regular branches, joined at turning points. Critical equilibria may be singular common level sets without destroying generic integrability. If $f$ is constant, the three <momenta> themselves give an everywhere independent commuting set, avoiding the rank loss of $p_x,p_y,H$ at $p_z=0$.