I’ll write the equations as
$$
\vec s+t\vec v+\frac12at^2\vec n_\theta=0\;,
$$
where $\vec s=\vec p-\vec g$ and $\vec n_\theta=(\cos\theta,\sin\theta)$ is the unit vector at angle $\theta$ with the positive $x$ axis.
This isn’t really an optimization problem. There are only discrete values of $\theta$ for which the target is reached at all. To find them, multiply the equation with $\vec u_\theta=\vec n_{\theta+\frac\pi2}$, a direction vector orthogonal to $\vec n_\theta$:
$$
\vec s\cdot\vec u_\theta+t\vec v\cdot\vec u_\theta=0
$$
and thus
$$
t=-\frac{\vec s\cdot\vec u_\theta}{\vec v\cdot\vec u_\theta}\;.
$$
Multiplying the original equation by $\vec n_\theta$, substituting $t$ from above and multiplying by $\vec v\cdot\vec u_\theta$ yields
$$
\left(\vec v\cdot\vec u_\theta\right)\left(\vec s\cdot\vec n_\theta\right)-\left(\vec v\cdot\vec n_\theta\right)\left(\vec s\cdot\vec u_\theta\right)+\frac12a\frac{\left(\vec s\cdot\vec u_\theta\right)^2}{\vec v\cdot\vec u_\theta}=0\;.
$$
If the first two terms are written out in terms of the trigonometric functions, half the terms cancel and the other half can be combined using $\cos^2+\sin^2=1$. Then multiplying by $\vec v\cdot\vec u_\theta$ again yields
$$
(v_ys_x-v_xs_y)\left(\vec v\cdot\vec u_\theta\right)+\frac12a\left(\vec s\cdot\vec u_\theta\right)^2=0\;.
$$
This is a trigonometric equation in $\theta$. You can turn it into a quartic algebraic equation either by writing it as an equation in $\mathrm e^{\mathrm i\theta}$ or by using $\sin^2=1-\cos^2$, bringing the terms proportional to $\sin\theta$ to one side, squaring and using $\sin^2=1-\cos^2$ again to obtain a quartic equation in $\cos\theta$. (The squaring introduces spurious solutions that you need to discard.) The solutions for which $t$ (as found above) is positive are the only values of $\theta$ for which the target is reached at all. If there is more than one such solution, the optimization consists merely in comparing the corresponding $t$ values. I suspect there will always be one solution for positive $t$ and one for negative $t$, but I’m not sure about that.