Solution (source code)

= Solution

The assumed relation makes the old and new actions differ by the endpoint term $G(t_f)-G(t_i)$. Fixed endpoint values of both coordinate sets make its variation zero. Therefore stationarity of the old <action> is equivalent to stationarity of
$$
\int\left(\sum_iP_i\dot Q_i-\mathcal H(Q,P,t)\right)dt.
$$
Varying independent $Q_i,P_i$ and integrating $P_i\delta\dot Q_i$ by parts gives
$$
\delta S=\int\sum_i[(\dot Q_i-\mathcal H_{P_i})\delta P_i
-(\dot P_i+\mathcal H_{Q_i})\delta Q_i]dt.
$$
Both coefficients vanish: \b[the new variables satisfy <Hamilton's equations>], so a locally invertible transformation with this <action> identity is canonical.