All right, I know there must be an elementary proof of this, but I am not sure why I never came across it before.
Adding a total time derivative to the Lagrangian (or a 4D divergence of some 4 vector in field theory) does not change the dynamics because the variation can be assumed to be zero on the boundary and integrated away.
But I don't see why any arbitrary function (as long as it is well behaved, no discontinuities, etc.) can't be written as a total derivative (or 4D divergence). In fact, I know that any nice scalar function in 3D can be written as a 3D divergence of some vector field, since for any 3D charge distribution, there exists an electric field whose divergence is equal to the charge function because of Gauss' Law.
But if I can write any function as a total derivative (or divergence of some vector) than I can add any function to the lagrangian and get the same dynamics, which means the lagrangian is completely arbitrary, which makes no sense at all.
So my question is, why can't an arbitrary function (as long as it is well behaved, no discontinuities, etc.) be written as a total derivative of some other function (or divergence of a vector)?