Solution (source code)

= Solution

Perform the <Legendre transform in mechanics>, using $\dot h_{ij}=2NK_{ij}+D_iN_j+D_jN_i$. The trace contraction $\pi^{ij}K_{ij}=\sqrt h(K_{ij}K^{ij}-K^2)$ gives
$$
H=\int d^3x\left[N\sqrt h(K_{ij}K^{ij}-K^2-{}^{(3)}R)+2\pi^{ij}D_iN_j\right].
$$
To avoid differentiating a <tensor density> as though it were an ordinary tensor, set $p^{ij}=\pi^{ij}/\sqrt h$. Integration by parts with the <Levi-Civita connection> of $h$ gives
$$
\int d^3x\sqrt h\,2p^{ij}D_iN_j=-\int d^3x\sqrt h\,2N^jD_ip^i{}_j
$$
after dropping the surface term. Also
$$
K_{ij}K^{ij}-K^2=\frac1h\left(\pi^{ij}\pi_{ij}-\frac12\pi^2\right),\qquad \pi_{ij}=h_{ik}h_{jl}\pi^{kl}.
$$
Therefore the requested <Hamiltonian> is
$$
\boxed{H=\int d^3x\sqrt h\,(N\mathcal H+N^i\mathcal H_i),\quad \mathcal H=\frac{\pi^{ij}\pi_{ij}-\pi^2/2}{h}-{}^{(3)}R,\quad \mathcal H_i=-2D_j\left(\frac{\pi^j{}_i}{\sqrt h}\right).}
$$
Varying the <lapse function> and <shift vector> imposes the <Hamiltonian constraint> $\mathcal H=0$ and <momentum constraint> $\mathcal H_i=0$. The total canonical generator also contains multipliers for the vanishing lapse and shift <canonical momenta>. With an asymptotic boundary, surface terms must be restored to define the <Arnowitt-Deser-Misner energy>.