  For a general subcritical second-order elliptic operator $P$ in a domain
$\Omega \subset \mathbb{R}^n$ (or noncompact manifold), we construct
Hardy-weight $W$ which is optimal in the following sense. The operator $P -
\lambda W$ is subcritical in $\Omega$ for all $\lambda < 1$, null-critical in
$\Omega$ for $\lambda = 1$, and supercritical near any neighborhood of infinity
in $\Omega$ for any $\lambda > 1$. Moreover, if $P$ is symmetric and $W>0$,
then the spectrum and the essential spectrum of $W^{-1}P$ are equal to
$[1,\infty)$, and the corresponding Agmon metric is complete.
  Our method is based on the theory of positive solutions and applies to both
symmetric and nonsymmetric operators. The constructed Hardy-weight is given by
an explicit simple formula involving two distinct positive solutions of the
equation $Pu=0$, the existence of which depends on the subcriticality of $P$ in
$\Omega$.
