Solution (source code)

= Solution

For a regular <Lagrangian>, form the <canonical momenta> $p_i=L_{\dot q_i}$, invert for $\dot q_i(\mathbf q,\mathbf p,t)$, and take the <Legendre transform> $H=\sum_ip_i\dot q_i-L$. On the upper cone let $s>0$ be distance along a generator from the apex and $\phi$ the <azimuth>. Then the horizontal radius is $s\sin\theta$ and height is $s\cos\theta$, so
$$
L=\frac m2(\dot s^2+s^2\sin^2\theta\,\dot\phi^2)-mgs\cos\theta,
\quad p_s=m\dot s,\quad p_\phi=ms^2\sin^2\theta\,\dot\phi.
$$
Consequently
$$
\boxed{H=\frac{p_s^2}{2m}+\frac{p_\phi^2}{2ms^2\sin^2\theta}+mgs\cos\theta.}
$$