Solution (source code)

= Solution

For <probability measures> $\mu,\nu$ on $\mathbb R^d$ with finite second <moments>, the <Knott–Smith optimality criterion> states that a <transport plan> $\pi\in\Pi(\mu,\nu)$ minimizes the quadratic cost $|x-y|^2$ if and only if there is a <proper convex function> $\Phi:\mathbb R^d\to\mathbb R\cup\{+\infty\}$ that is <sequentially lower semicontinuous> and satisfies
$$
\boxed{y\in\partial\Phi(x)\quad\text{for }\pi\text{-almost every }(x,y).}
$$
The <subdifferential> is characterized by
$$
y\in\partial\Phi(x)\quad\Longleftrightarrow\quad
\Phi(z)\geq\Phi(x)+y\cdot(z-x)\quad\text{for every }z\in\mathbb R^d,
$$
with $\Phi(x)<\infty$. Thus the <transport plan> is concentrated on the graph of the <subdifferential>. No <absolute continuity of measures> assumption on $\mu$ is needed. Multiplying the cost by $1/2$ leaves the criterion unchanged. The original quadratic optimal mapping result is https://link.springer.com/article/10.1007/BF00934745[Knott and Smith, On the optimal mapping of distributions].