I don't get this “derivation” of canonical transformation

Given a transformation $$(q, p, t)\to (Q(q, p, t), P(q, p, t), t),$$ the modified Hamiltonian, $$K$$ is related to the original one, $$H$$, as $$H(q, p, t) = K(Q(q, p, t), P(q, p, t), t).$$ Now, what I've seen done everywhere$$^1$$ is to heuristically cook up some unjustified extremization principle for the Hamiltonians, demanding that $$\int (p_i\dot q_i-H(q, p, t))\; dt,\\ \int(P_i \dot Q_i-K(Q, P, t))\;dt$$ be extremized. And this is followed by saying that a sufficient condition for them to lead to to the same equations of motion is that they differ by some total time derivative, and hence are born the generating functions.

However, there are several things that I don't understand at all:

1. The statement that seems to being (mis)used here is that if a trajectory $$q(t)$$ extremizes the integral $$\int_{t_0}^{t_f} L(q(t), \dot q(t), t) dt$$, then the same trajectory extremizes the integral if $$\frac{\partial f}{\partial q}(q, t)\dot q+\frac{\partial f}{\partial t}(q, t)$$ is added to $$L(q, \dot q, t)$$. Question: Is this justly used in the above? If so, how? There is no $$p$$ or $$P$$ in Lagrangian!
2. I don't even understand the claimed extremization principle. Question: How can one even go about extremizing this? Are we to perturb $$(q(t), p(t))$$ by two functions, $$\delta q(t)$$ and $$\delta p(t)$$? I'm familiar only with the action extremization principle that gives rise to E-L equations.

$$^1$$For example, these notes. (Section 4.1, Eq. (4.7-4.9).)

• Let us start with OP's 2nd point. Yes, in phase space positions $$q^i$$ and momenta $$p_i$$ are $$2n$$ independent variables, where $$i\in\{1,\ldots, n\}$$. Let's introduce a collective notation $$(z^1,\ldots,z^{2n})~=~(q^1,\ldots,q^n,p_1,\ldots p_n).$$
• Concerning OP's 1st point, the main issue is that one should use the phase space variables $$z^I$$, not the configuration space variables $$q^i$$.
• Euler-Lagrange (EL) equations for $$z^I$$, $$I\in\{1,\ldots, 2n\}$$, are precisely Hamilton's equations, cf. e.g. this Phys.SE post.
• A total time derivative term does not change EL equation, cf. e.g. this Phys.SE post (but now applied to $$z$$ rather than $$q$$).
• So in point 1, $L$ should be viewed as $L(z, \dot z, t)$? – Atom Feb 19 at 9:41
• $\uparrow$ Yes. – Qmechanic Feb 19 at 9:47