Solution (source code)

= Solution

If $L'=L+dF(q,t)/dt$, the <action> changes by an endpoint term:
$$
S'[q]-S[q]=F(q(t_f),t_f)-F(q(t_i),t_i).
$$
In the <principle of stationary action> with fixed endpoints, $\delta q(t_i)=\delta q(t_f)=0$ and the endpoint times are fixed. Therefore $\delta(S'-S)=0$, and the two actions give the same bulk <Euler-Lagrange equations>.

For a <Lagrangian density>, adding $\partial_\mu K^\mu$ changes the <action> by $\int_{\partial\Omega}K^\mu n_\mu\,d\Sigma$, by the <divergence theorem>. Its variation vanishes for compactly supported field variations, or when the appropriate boundary data are held fixed. These <total divergences in a variational principle> can therefore be discarded when deriving bulk equations under those conditions.

The qualification about boundaries matters: if $K^\mu$ depends on field derivatives, their variations on the boundary must also be controlled. A <total derivative> can change the canonical momenta or the chosen representative of a <Noether current>, even while leaving the bulk dynamics unchanged.