[step:Diagonalize the Selberg quadratic form and compute its minimum]Since $0\leq g(p)<1$ for every prime $p<z$, each $h(d)$ is finite and nonnegative. Since $1\in\mathcal D$ and $h(1)=1$, we have $G(D,z)>0$.
[claim:The optimal Selberg weights satisfy $Q(\lambda)=1/G(D,z)$]
For every $\mu\in\mathbb R^{\mathcal D}$ satisfying $\mu_1=1$,
\begin{align*}
Q(\mu)\geq \frac{1}{G(D,z)}.
\end{align*}
The unique minimizer is the optimal Selberg weight vector $\lambda$, and it satisfies
\begin{align*}
Q(\lambda)=\frac{1}{G(D,z)}.
\end{align*}
[/claim]
[proof]
Define $b:\mathcal D\to\mathbb R$ by
\begin{align*}
b(r):=\prod_{p\mid r}(1-g(p)).
\end{align*}
For each $\mu\in\mathbb R^{\mathcal D}$, define $y(\mu):\mathcal D\to\mathbb R$ by
\begin{align*}
y_r(\mu):=b(r)\sum_{\substack{d\in\mathcal D: r\mid d}}g(d/r)\mu_d.
\end{align*}
This is well-defined because $\mathcal D$ is closed under divisors, and if $r\mid d$ with $r,d\in\mathcal D$, then $d/r$ is squarefree and divides $P(z)$.
For $r,d\in\mathcal D$, define $c_{r,d}:=0$ if $r\nmid d$, and define
\begin{align*}
c_{r,d}:=b(r)g(d/r)
\end{align*}
if $r\mid d$. We verify the factorization
\begin{align*}
g(\operatorname{lcm}(d,e))=\sum_{r\in\mathcal D}h(r)c_{r,d}c_{r,e}
\end{align*}
for fixed $d,e\in\mathcal D$. The function $g$ is multiplicative on squarefree divisors and $g(1)=1$, so it is enough to compare the local contribution of each prime dividing $P(z)$. If a prime $p$ divides neither $d$ nor $e$, the local contribution is $1$. If $p$ divides exactly one of $d,e$, only the local choice with $p\nmid r$ contributes, and the local contribution is $g(p)$. If $p$ divides both $d$ and $e$, the local contribution from $p\nmid r$ is $g(p)^2$, while the local contribution from $p\mid r$ is
\begin{align*}
\frac{g(p)}{1-g(p)}(1-g(p))^2=g(p)(1-g(p)).
\end{align*}
The sum in this last case is $g(p)$. Thus the product of the local factors is exactly $g(\operatorname{lcm}(d,e))$.
Substituting the factorization into $Q(\mu)$ and collecting terms gives the diagonal identity
\begin{align*}
Q(\mu)=\sum_{r\in\mathcal D}h(r)y_r(\mu)^2.
\end{align*}
The finite inclusion-exclusion identity on the divisor poset gives
\begin{align*}
\mu_1=\sum_{r\in\mathcal D}\mu_{\mathrm{Mob}}(r)h(r)y_r(\mu),
\end{align*}
where $\mu_{\mathrm{Mob}}$ denotes the ordinary Mobius function. Indeed, the coefficient of $\mu_d$ in the right-hand side is
\begin{align*}
\sum_{r\mid d}\mu_{\mathrm{Mob}}(r)h(r)b(r)g(d/r).
\end{align*}
Since $h(r)b(r)=g(r)$ and $g(r)g(d/r)=g(d)$ for $r\mid d$, this coefficient equals
\begin{align*}
g(d)\sum_{r\mid d}\mu_{\mathrm{Mob}}(r).
\end{align*}
The divisor sum of the Mobius function is $1$ when $d=1$ and $0$ when $d>1$, proving the identity.
Now assume $\mu_1=1$. Applying the [Cauchy-Schwarz Inequality](/theorems/432) to the finite real vectors $(h(r)^{1/2}y_r(\mu))_{r\in\mathcal D}$ and $(\mu_{\mathrm{Mob}}(r)h(r)^{1/2})_{r\in\mathcal D}$ gives
\begin{align*}
1\leq \left(\sum_{r\in\mathcal D}h(r)y_r(\mu)^2\right)\left(\sum_{r\in\mathcal D}h(r)\right).
\end{align*}
Using the diagonal identity and the definition of $G(D,z)$, this becomes
\begin{align*}
Q(\mu)\geq \frac{1}{G(D,z)}.
\end{align*}
Equality holds precisely when the two Cauchy-Schwarz vectors are proportional. Taking
\begin{align*}
y_r=\frac{\mu_{\mathrm{Mob}}(r)}{G(D,z)}
\end{align*}
for every $r\in\mathcal D$ gives the constraint
\begin{align*}
\sum_{r\in\mathcal D}\mu_{\mathrm{Mob}}(r)h(r)y_r=1
\end{align*}
and equality in Cauchy-Schwarz. The change of variables from $\mu$ to $y(\mu)$ is triangular with nonzero diagonal entries $b(r)$, because every factor $1-g(p)$ is positive. Hence there is a unique vector $\lambda\in\mathbb R^{\mathcal D}$ with $\lambda_1=1$ giving these $y$-values. By the definition in the theorem statement, this vector is the optimal Selberg weight vector. Therefore
\begin{align*}
Q(\lambda)=\frac{1}{G(D,z)}.
\end{align*}
[/proof][/step]