  Let $p \in (1,\infty)$ and $\Omega \subset \mathbb{R}^N$ be a domain. Let $
A: =(a_{ij}) \in L^{\infty}_{\text{loc}}(\Omega; \mathbb{R}^{N\times N})$ be a
symmetric and locally uniformly positive definite matrix. Set $|\xi|_A^2:=
\displaystyle \sum_{i,j=1}^N a_{ij}(x) \xi_i \xi_j$, $\xi \in \mathbb{R}^N$,
and let $V$ be a given potential in a certain local Morrey space. We assume
that the energy functional
  $$Q_{p,A,V}(\phi):=\displaystyle \int_{\Omega} [|\nabla \phi|_A^p +
V|\phi|^p] {\rm dx} $$ is nonnegative in $W^{1,p}(\Omega)\cap C_c(\Omega)$.
  We introduce a generalized notion of $Q_{p,A,V}$-capacity and characterize
the space of all Hardy-weights for the functional $Q_{p,A,V}$, extending
Maz'ya's well known characterization of the space of Hardy-weights for the
$p$-Laplacian. In addition, we provide various sufficient conditions on the
potential $V$ and the Hardy-weight $g$ such that the best constant of the
corresponding variational problem is attained in an appropriate Beppo-Levi
space.
