Consider the symmetric exclusion process evolving on an interval and weakly interacting at the end-points with reservoirs. Denote by $I_{[0,T]} (cdot)$ its dynamical large deviations functional and by $V(cdot)$ the associated quasi-potential, defined as $V(gamma) = inf_{T>0} inf_u I_{[0,T]} (u)$, where the infimum is carried over all trajectories $u$ such that $u(0) = barrho$, $u(T) = gamma$, and $barrho$ is the stationary density profile. We derive the partial differential equation which describes the evolution of the optimal trajectory, and deduce from this result the formula obtained by Derrida, Hirschberg and Sadhu cite{DHS2021} for the quasi-potential through the representation of the steady state as a product of matrices.