[proofplan]
We diagonalize the Selberg quadratic form by replacing the weights $\lambda_d$ with divisor-sum variables $y_r$. The kernel $g([d,e])$, where $[d,e]$ denotes the least common multiple of $d$ and $e$, factors over primes, and this gives an exact identity expressing $Q(\lambda)$ as a sum of squares $\sum y_r^2/h(r)$. The normalization $\lambda_1=1$ becomes the linear constraint $\sum y_r=1$, so a one-line square-completion gives the lower bound $Q(\lambda)\geq G(D,z)^{-1}$. Finally, we verify directly that the displayed weights produce the equality variables $y_r=h(r)/G(D,z)$.
[/proofplan]
[step:Introduce the finite divisor set and the diagonal variables]
Let $\mu:\mathbb{N}\to\{-1,0,1\}$ denote the Möbius function. Since $D>1$, the integer $1$ belongs to the finite divisor set
\begin{align*}
\mathcal{S} := \{d \in \mathbb{N} : d<D,\ d\mid P(z)\}.
\end{align*}
Let
\begin{align*}
\mathcal{S}_+ := \{d \in \mathcal{S} : h(d)>0\}.
\end{align*}
Since $h(1)=1$, the set $\mathcal{S}_+$ is non-empty. A Selberg weight of level $D$ is a map $\lambda:\mathcal{S}\to\mathbb{R}$ satisfying $\lambda_1=1$, and its quadratic form is
\begin{align*}
Q(\lambda):=\sum_{d\in\mathcal{S}}\sum_{e\in\mathcal{S}}\lambda_d\lambda_e g([d,e]).
\end{align*}
For a Selberg weight $\lambda:\mathcal{S}\to\mathbb{R}$ with $\lambda_1=1$, define the map $x(\lambda):\mathcal{S}\to\mathbb{R}$ by
\begin{align*}
x_r(\lambda) := \sum_{d\in\mathcal{S},\ r\mid d} \lambda_d g(d)
\end{align*}
for every $r\in\mathcal{S}$. Define the map $y(\lambda):\mathcal{S}\to\mathbb{R}$ by
\begin{align*}
y_r(\lambda) := \mu(r)x_r(\lambda)
\end{align*}
for every $r\in\mathcal{S}$.
By the definition of $h$, for every $r\in\mathcal{S}$ one has $h(r)>0$ exactly when every prime $p\mid r$ satisfies $g(p)>0$. Thus, if $r\notin\mathcal{S}_+$, then some prime $p\mid r$ has $g(p)=0$. Every $d\in\mathcal{S}$ with $r\mid d$ then satisfies $g(d)=0$ by multiplicativity of $g$, so $x_r(\lambda)=0$ and $y_r(\lambda)=0$.
Define the map $A:\mathcal{S}\to\mathbb{R}$ by $A_d:=\lambda_d g(d)$ for $d\in\mathcal{S}$. The finite Möbius identity needed here follows by expanding the divisor sums:
\begin{align*}
\sum_{r\in\mathcal{S}}\mu(r)x_r(\lambda)=\sum_{d\in\mathcal{S}}A_d\sum_{r\mid d}\mu(r).
\end{align*}
Since every $d\in\mathcal{S}$ is squarefree, the inner sum is $\prod_{p\mid d}(1-1)$, interpreted as $1$ when $d=1$, and is therefore zero for $d>1$. Hence
\begin{align*}
A_1=\sum_{r\in\mathcal{S}}\mu(r)x_r(\lambda).
\end{align*}
Since $A_1=\lambda_1g(1)=1$ by the Selberg-weight normalization $\lambda_1=1$ and the stated convention $g(1)=1$, and since $y_r(\lambda)=0$ outside $\mathcal{S}_+$, this gives the normalization
\begin{align*}
\sum_{r\in\mathcal{S}_+} y_r(\lambda)=1.
\end{align*}
[guided]
The purpose of the variables $y_r$ is to separate the interactions between two divisors $d$ and $e$ into independent square terms indexed by their common divisors. We work on the finite divisor set
\begin{align*}
\mathcal{S} := \{d \in \mathbb{N} : d<D,\ d\mid P(z)\}.
\end{align*}
The active part is
\begin{align*}
\mathcal{S}_+ := \{d \in \mathcal{S} : h(d)>0\}.
\end{align*}
This restriction matters only when some prime has $g(p)=0$. If $r\notin\mathcal{S}_+$, then $r$ contains a prime $p$ with $g(p)=0$, and every multiple $d$ of $r$ has $g(d)=0$ by multiplicativity. Hence such $r$ contributes no active diagonal term.
For each Selberg weight $\lambda:\mathcal{S}\to\mathbb{R}$, define the map $x(\lambda):\mathcal{S}\to\mathbb{R}$ by
\begin{align*}
x_r(\lambda) := \sum_{d\in\mathcal{S},\ r\mid d} \lambda_d g(d).
\end{align*}
Then define the signed diagonal-variable map $y(\lambda):\mathcal{S}\to\mathbb{R}$ by
\begin{align*}
y_r(\lambda) := \mu(r)x_r(\lambda).
\end{align*}
The sign $\mu(r)$ is chosen so that the condition $\lambda_1=1$ becomes a positive-looking linear constraint.
Indeed, define the map $A:\mathcal{S}\to\mathbb{R}$ by $A_d:=\lambda_d g(d)$ for $d\in\mathcal{S}$. The definition of $x_r$ is exactly
\begin{align*}
x_r(\lambda)=\sum_{d\in\mathcal{S},\ r\mid d} A_d.
\end{align*}
The finite Möbius identity follows directly in this squarefree divisor poset. Expanding the finite sums gives
\begin{align*}
\sum_{r\in\mathcal{S}}\mu(r)x_r(\lambda)=\sum_{d\in\mathcal{S}}A_d\sum_{r\mid d}\mu(r).
\end{align*}
For squarefree $d$, the inner divisor sum is $\prod_{p\mid d}(1-1)$, with the empty product equal to $1$. Hence it equals $1$ for $d=1$ and $0$ for $d>1$, so
\begin{align*}
A_1=\sum_{r\in\mathcal{S}}\mu(r)x_r(\lambda).
\end{align*}
Since $g(1)=1$ by the stated convention and $\lambda_1=1$ by the definition of Selberg weight, the left-hand side is $A_1=1$. Also, if $r\notin\mathcal{S}_+$ then $x_r(\lambda)=0$, so the inactive terms vanish. Therefore
\begin{align*}
\sum_{r\in\mathcal{S}_+} y_r(\lambda)=\sum_{r\in\mathcal{S}_+}\mu(r)x_r(\lambda)=1.
\end{align*}
This is the linear constraint that will pair with the diagonalized quadratic form.
[/guided]
[/step]
[step:Factor the kernel $g([d,e])$ into common-divisor contributions]
For $d,e\in\mathcal{S}$, we claim that
\begin{align*}
g([d,e])=g(d)g(e)\sum_{r\mid d,\ r\mid e,\ h(r)>0}\frac{1}{h(r)}.
\end{align*}
Because $d$ and $e$ are squarefree divisors of $P(z)$, every prime occurs in each of $d$ and $e$ with exponent either $0$ or $1$. Both sides are multiplicative in the primes dividing $de$. If a prime $p$ divides $d$ or $e$ and $g(p)=0$, then both sides vanish. Otherwise it is enough to check the three local cases. If $p$ divides neither $d$ nor $e$, the local factor is $1$. If $p$ divides exactly one of $d,e$, the local factor is $g(p)$. If $p$ divides both, then the right-hand local factor is
\begin{align*}
g(p)^2\left(1+\frac{1}{h(p)}\right)=g(p)^2\left(1+\frac{1-g(p)}{g(p)}\right)=g(p).
\end{align*}
This is the local factor of $g([d,e])$ in every case, so the identity follows.
[guided]
We prove the kernel factorization prime by prime. This is valid because $d$ and $e$ are squarefree divisors of $P(z)$, and both $g$ and $h$ are multiplicative on squarefree divisors. The identity to prove is
\begin{align*}
g([d,e])=g(d)g(e)\sum_{r\mid d,\ r\mid e,\ h(r)>0}\frac{1}{h(r)}.
\end{align*}
First suppose a prime $p$ divides $d$ or $e$ and $g(p)=0$. Then $g([d,e])=0$ by multiplicativity. On the right-hand side, if $p$ divides $d$ or $e$ but not both, then the factor $g(d)g(e)$ is already zero; if $p$ divides both, then every common divisor term that contains $p$ is excluded by $h(p)=0$, while the terms not containing $p$ are still multiplied by $g(d)g(e)=0$. Thus both sides vanish in this case.
It remains to check primes with $g(p)>0$. If $p$ divides neither $d$ nor $e$, both local factors are $1$. If $p$ divides exactly one of $d,e$, the right-hand side contributes the local factor $g(p)$ from $g(d)g(e)$ and no local contribution from the common-divisor sum, matching $g([d,e])$. If $p$ divides both $d$ and $e$, then the right-hand local factor is
\begin{align*}
g(p)^2\left(1+\frac{1}{h(p)}\right)=g(p)^2\left(1+\frac{1-g(p)}{g(p)}\right)=g(p).
\end{align*}
Here we used the structural definition $h(p)=g(p)/(1-g(p))$, which is legitimate because $0<g(p)<1$ in this case. This again matches the local factor of $g([d,e])$. Multiplying the verified local identities over the finitely many primes dividing $de$ proves the displayed kernel identity.
[/guided]
[/step]
[step:Diagonalize the quadratic form]
Using the kernel identity and finite rearrangement of sums, we get
\begin{align*}
Q(\lambda)=\sum_{d\in\mathcal{S}}\sum_{e\in\mathcal{S}}\lambda_d\lambda_e g(d)g(e)\sum_{r\mid d,\ r\mid e,\ h(r)>0}\frac{1}{h(r)}.
\end{align*}
Interchanging the finite sums gives
\begin{align*}
Q(\lambda)=\sum_{r\in\mathcal{S}_+}\frac{1}{h(r)}\left(\sum_{d\in\mathcal{S},\ r\mid d}\lambda_d g(d)\right)^2.
\end{align*}
By the definition of $x_r(\lambda)$ and $y_r(\lambda)=\mu(r)x_r(\lambda)$, and since $\mu(r)^2=1$ on squarefree divisors of $P(z)$, this becomes
\begin{align*}
Q(\lambda)=\sum_{r\in\mathcal{S}_+}\frac{y_r(\lambda)^2}{h(r)}.
\end{align*}
[/step]
[step:Minimize the diagonal form under the linear constraint]
Let
\begin{align*}
G:=G(D,z)=\sum_{r\in\mathcal{S}}h(r)=\sum_{r\in\mathcal{S}_+}h(r).
\end{align*}
For every Selberg weight $\lambda$ with $\lambda_1=1$, the previous steps give
\begin{align*}
Q(\lambda)-\frac{1}{G}=\sum_{r\in\mathcal{S}_+}\frac{1}{h(r)}\left(y_r(\lambda)-\frac{h(r)}{G}\right)^2.
\end{align*}
Indeed, expanding the right-hand side gives
\begin{align*}
\sum_{r\in\mathcal{S}_+}\frac{y_r(\lambda)^2}{h(r)}-\frac{2}{G}\sum_{r\in\mathcal{S}_+}y_r(\lambda)+\frac{1}{G^2}\sum_{r\in\mathcal{S}_+}h(r).
\end{align*}
Using $\sum_{r\in\mathcal{S}_+}y_r(\lambda)=1$ and $\sum_{r\in\mathcal{S}_+}h(r)=G$, this equals $Q(\lambda)-G^{-1}$. Since every square on the right-hand side is non-negative, we obtain
\begin{align*}
Q(\lambda)\geq \frac{1}{G(D,z)}.
\end{align*}
Equality holds exactly when
\begin{align*}
y_r(\lambda)=\frac{h(r)}{G(D,z)}
\end{align*}
for every $r\in\mathcal{S}_+$.
[guided]
After diagonalization, the only remaining problem is a finite constrained minimization. Define
\begin{align*}
G:=G(D,z)=\sum_{r\in\mathcal{S}}h(r)=\sum_{r\in\mathcal{S}_+}h(r).
\end{align*}
The second equality holds because $h(r)=0$ for $r\in\mathcal{S}\setminus\mathcal{S}_+$. Also $G>0$ because $1\in\mathcal{S}_+$ and $h(1)=1$.
For every admissible Selberg weight $\lambda$, the preceding steps give
\begin{align*}
Q(\lambda)=\sum_{r\in\mathcal{S}_+}\frac{y_r(\lambda)^2}{h(r)}
\end{align*}
and
\begin{align*}
\sum_{r\in\mathcal{S}_+}y_r(\lambda)=1.
\end{align*}
We complete the square around the candidate minimizer $y_r=h(r)/G$. Expanding the finite sum gives
\begin{align*}
\sum_{r\in\mathcal{S}_+}\frac{1}{h(r)}\left(y_r(\lambda)-\frac{h(r)}{G}\right)^2=\sum_{r\in\mathcal{S}_+}\frac{y_r(\lambda)^2}{h(r)}-\frac{2}{G}\sum_{r\in\mathcal{S}_+}y_r(\lambda)+\frac{1}{G^2}\sum_{r\in\mathcal{S}_+}h(r).
\end{align*}
Substituting the normalization and the definition of $G$ turns the right-hand side into $Q(\lambda)-G^{-1}$. The left-hand side is a sum of non-negative [real numbers](/page/Real%20Numbers) because $h(r)>0$ on $\mathcal{S}_+$. Therefore $Q(\lambda)\ge G^{-1}$, and equality holds precisely when every square is zero, namely when
\begin{align*}
y_r(\lambda)=\frac{h(r)}{G}
\end{align*}
for all $r\in\mathcal{S}_+$.
[/guided]
[/step]
[step:Verify that the displayed weights attain the equality variables]
Define $\lambda:\mathcal{S}\to\mathbb{R}$ by
\begin{align*}
\lambda_d := \mu(d)\prod_{p\mid d}(1-g(p))^{-1}\frac{G_d(D,z)}{G(D,z)}.
\end{align*}
For $d=1$, this gives
\begin{align*}
\lambda_1=\frac{G_1(D,z)}{G(D,z)}=1.
\end{align*}
Thus $\lambda$ is an admissible Selberg weight.
Fix $r\in\mathcal{S}_+$. Since $h(r)>0$, every prime dividing $r$ has $g(p)>0$. We compute
\begin{align*}
x_r(\lambda)=\sum_{d\in\mathcal{S},\ r\mid d}\lambda_d g(d).
\end{align*}
Write each such $d$ uniquely as $d=ra$ with $a<D/r$, $(a,r)=1$, and $a\mid P(z)$. Then
\begin{align*}
\lambda_{ra}g(ra)=\frac{\mu(r)\mu(a)h(r)h(a)G_{ra}(D,z)}{G(D,z)}.
\end{align*}
Therefore
\begin{align*}
x_r(\lambda)=\frac{\mu(r)h(r)}{G(D,z)}\sum_{a<D/r,\ (a,r)=1,\ a\mid P(z)}\mu(a)h(a)G_{ra}(D,z).
\end{align*}
By definition,
\begin{align*}
G_{ra}(D,z)=\sum_{m<D/(ra),\ (m,ra)=1,\ m\mid P(z)}h(m).
\end{align*}
Thus the inner sum equals
\begin{align*}
\sum_{a<D/r,\ (a,r)=1,\ a\mid P(z)}\sum_{m<D/(ra),\ (m,ra)=1,\ m\mid P(z)}\mu(a)h(a)h(m).
\end{align*}
Since $a$ and $m$ are coprime squarefree divisors of $P(z)$, multiplicativity gives $h(a)h(m)=h(am)$. Grouping terms by $n=am$, the inner sum becomes
\begin{align*}
\sum_{n<D/r,\ (n,r)=1,\ n\mid P(z)}h(n)\sum_{a\mid n}\mu(a).
\end{align*}
Since $n$ is squarefree, the divisor sum satisfies
\begin{align*}
\sum_{a\mid n}\mu(a)=\prod_{p\mid n}(1-1).
\end{align*}
This product is the empty product $1$ when $n=1$ and is $0$ when $n>1$. Hence the inner sum is $1$, and so
\begin{align*}
x_r(\lambda)=\frac{\mu(r)h(r)}{G(D,z)}.
\end{align*}
Multiplying by $\mu(r)$ gives
\begin{align*}
y_r(\lambda)=\frac{h(r)}{G(D,z)}.
\end{align*}
Thus the equality condition from the preceding step holds for every $r\in\mathcal{S}_+$, and consequently
\begin{align*}
Q(\lambda)=\frac{1}{G(D,z)}.
\end{align*}
This proves both the optimal value and the asserted formula for an attaining system of Selberg weights.
[guided]
We now check that the displayed formula really gives the equality case, rather than only guessing it from the completed square. Define the map $\lambda:\mathcal{S}\to\mathbb{R}$ by
\begin{align*}
\lambda_d := \mu(d)\prod_{p\mid d}(1-g(p))^{-1}\frac{G_d(D,z)}{G(D,z)}.
\end{align*}
For $d=1$, the product over primes is the empty product and $G_1(D,z)=G(D,z)$, so
\begin{align*}
\lambda_1=1.
\end{align*}
Thus $\lambda$ is a Selberg weight of level $D$.
Fix $r\in\mathcal{S}_+$. The condition $h(r)>0$ means every prime divisor of $r$ satisfies $g(p)>0$, so the factors $1-g(p)$ are non-zero. We compute the diagonal variable
\begin{align*}
x_r(\lambda)=\sum_{d\in\mathcal{S},\ r\mid d}\lambda_d g(d).
\end{align*}
Every divisor $d\in\mathcal{S}$ with $r\mid d$ has a unique form $d=ra$, where $a<D/r$, $(a,r)=1$, and $a\mid P(z)$. Since $r$ and $a$ are coprime squarefree divisors, multiplicativity of $\mu$, $g$, and $h$ gives
\begin{align*}
\lambda_{ra}g(ra)=\frac{\mu(r)\mu(a)h(r)h(a)G_{ra}(D,z)}{G(D,z)}.
\end{align*}
Therefore
\begin{align*}
x_r(\lambda)=\frac{\mu(r)h(r)}{G(D,z)}\sum_{a<D/r,\ (a,r)=1,\ a\mid P(z)}\mu(a)h(a)G_{ra}(D,z).
\end{align*}
Now expand the definition of $G_{ra}(D,z)$:
\begin{align*}
G_{ra}(D,z)=\sum_{m<D/(ra),\ (m,ra)=1,\ m\mid P(z)}h(m).
\end{align*}
Substituting this expression, the inner sum becomes
\begin{align*}
\sum_{a<D/r,\ (a,r)=1,\ a\mid P(z)}\sum_{m<D/(ra),\ (m,ra)=1,\ m\mid P(z)}\mu(a)h(a)h(m).
\end{align*}
The coprimality condition $(m,ra)=1$ ensures in particular that $(a,m)=1$, so multiplicativity gives $h(a)h(m)=h(am)$. Grouping by $n=am$ gives
\begin{align*}
\sum_{n<D/r,\ (n,r)=1,\ n\mid P(z)}h(n)\sum_{a\mid n}\mu(a).
\end{align*}
For squarefree $n$, the Möbius divisor sum is
\begin{align*}
\sum_{a\mid n}\mu(a)=\prod_{p\mid n}(1-1),
\end{align*}
which equals $1$ for $n=1$ and $0$ for $n>1$. Hence the grouped sum equals $1$, and we obtain
\begin{align*}
x_r(\lambda)=\frac{\mu(r)h(r)}{G(D,z)}.
\end{align*}
Multiplying by $\mu(r)$ and using $\mu(r)^2=1$ for squarefree $r$ gives
\begin{align*}
y_r(\lambda)=\frac{h(r)}{G(D,z)}.
\end{align*}
This is exactly the equality condition from the completed-square step for every $r\in\mathcal{S}_+$. Consequently the displayed weights attain
\begin{align*}
Q(\lambda)=\frac{1}{G(D,z)}.
\end{align*}
[/guided]
[/step]