Let $g_0^\prime(x)=|x-1/2|,\; g_0(x)=\int_0^xg_1(t)dt,\; 0\leq x\leq1$. Then $g_0$ has continuus derivative, namely $g_0^\prime$, which is Lipschitz, but the second derivative is discontinuous. Now define $f(x,y)=0$ when $0\leq x\leq 1, y=g_0(x)$ and $f(x,y)=1$ otherwise. Evidently $f$ is measurable, and $\int_0^1 f(x,g_0(x))dx=0$, while $\int_0^1f(x,g(x))dx>0$ for every $C^2$ function $g$. Remark. You can make $f$ continuous and even smooth by taking a non-negative smooth function with the same zero set as I described. Even the polynomial $f(x,y)=(x-y)^2(x+y-1)^2$ will do the job.