Analytic solutions and triality theory for nonconvex and nonsmooth variational problems with applications