A variational representation for certain functionals of Brownian motion