Since the utility function has the Leontief form, then the two goods are perfect
complements. Therefore the consumer will always choose the kink point where
$a_1x_1 = a_2x_2$, i.e. the maxima $(x_1^*,x_2^*)$ satises $a_1x_1^* = a_2x_2^*$.
Also since the consumer will spend all his/her income, we can have two variables
$(x_1^*,x_2^*)$ and two equalities:
$$\left\{
\begin{align}
a_1x_1^* &= a_2x_2^*\\
p_1x_1^* +p_2x_2^*&=w\\
\end{align}\right.
$$
Therefore by solving the two equations, we can nd the demand function for each good:
$$\left\{
\begin{align}
x_1^* &= \frac{a_2 w}{a_2p_1+a_1p_2}\\
x_2^*&=\frac{a_1 w}{a_2p_1+a_1p_2}\\
\end{align}\right.
$$
Note that we cannot equate the MRS with the slope of the budget line here, because the MRS is not deļ¬ned at the point where $a_1x_1 = a_2x_2$.
Hint: First, start off with the constrained maximization problem and then use the Lagrangian method to get the first-order conditions. Then use the Kuhn-Tucker method (see Mathematics for Economist by Simon and Blume) to solve for your optimal demands.
The consumer solves the following maximization problem:
\begin{align*}
\max_{q_1,q_2} \alpha_1q_1 + \alpha_2q_2 - \frac{1}{2}(q_1^2+q_2^2+2\epsilon q_1 q_2) \\
\text{s.t. } p_1 q_1 + p_2q_2 \le m.
\end{align*}
The Lagrangian associated with maximization problem is
\begin{equation*}
\mathcal{L} = \alpha_1q_1 + \alpha_2q_2 - \frac{1}{2}(q_1^2+q_2^2+2\epsilon q_1 q_2) + \lambda[m - p_1 q_1 - p_2q_2]
\end{equation*}
Then take the derivatives of $\mathcal{L}$ wrt to $q_{1}$, $q_{2}$, and $\lambda$ to get the following first-order conditions:
\begin{equation}
\frac{\partial \mathcal{L}}{\partial q_{1}}=\alpha_{1}-q_{1}+\epsilon q_{2} -\lambda p_{1} =0
\end{equation}
\begin{equation}
\frac{\partial \mathcal{L}}{\partial q_{2}}=\alpha_{2}-q_{2}+\epsilon q_{1} -\lambda p_{2}=0
\end{equation}
\begin{equation}
\frac{\partial \mathcal{L}}{\partial \lambda}=m-p_{1}q_{1}-p_{2}q_{2}\geq0
\end{equation}
Then we use the Kuhn-Tucker method:
$\lambda[m-p_{1}q_{1}-p_{2}q_{2}] = 0$ is the complementary slackness condition on $\lambda$
and
$\lambda\geq0$ is the non-negativity constraint on $\lambda$.
We can have two cases: either $\lambda>0$ or $\lambda=0$. I show what happens for $\lambda>0$ and then you can check what happens for $\lambda=0$.
If $\lambda>0$ then $m-p_{1}q_{1}-p_{2}q_{2}=0$ by the complementary slackness condition. In other words, this is the case where the budget constraint is binding, that is, $m=p_{1}q_{1}-p_{2}q_{2}$. Then we check whether the first-order conditions and the non-negativity condition hold.
After doing so we are left with the following set of equations:
\begin{equation}
\frac{\partial \mathcal{L}}{\partial q_{1}}=\alpha_{1}-q_{1}+\epsilon q_{2} -\lambda p_{1} =0
\end{equation}
\begin{equation}
\frac{\partial \mathcal{L}}{\partial q_{2}}=\alpha_{2}-q_{2}+\epsilon q_{1} -\lambda p_{2}=0
\end{equation}
\begin{equation}
m-p_{1}q_{1}-m_{2}q_{2}=0
\end{equation}
Then substitute away $\lambda$ from the first two equations and solve for $q_{1}$ and $q_{2}$ using the binding budget constraint.
Edit: For the unconstrained problem you have no budget constraint so your set up is conceptually wrong. The way you get an unconstrained problem from a constrained problem is that you substitute away your budget constraint in the utility function. Also I haven't imposed non-negative conditions on $q_{1}$ and $q_{2}$, as then the process would be quite tedious.
Best Answer
A clever way to solve this kind of problems (with a Cobb-Douglas function) is as follows:
$ax_1^{a-1}x_2^{1-a} - \lambda p_1 = 0$
$x_1^a(1-a)x_2^{-a} - \lambda p_2 = 0$
Bringing the terms involving $\lambda$ to the RHS:
$ax_1^{a-1}x_2^{1-a} = \lambda p_1 $
$x_1^a(1-a)x_2^{-a} = \lambda p_2 $
Dividing the first equation by the second equation:
$\frac{ax_1^{a-1}x_2^{1-a}}{x_1^a(1-a)x_2^{-a}}=\frac{\lambda p_1}{\lambda p_2}$
$\lambda $ can be cancelled. In addition you can cancel out $x_1^{\alpha}$ and $x_2^{-\alpha}$.
$\frac{ax_1^{-1}x_2^{1}}{(1-a)}=\frac{ p_1}{ p_2}$
$\frac{ax_2^{1}}{(1-a)x_1^{1}}=\frac{ p_1}{ p_2}$
The exponents are not needed anymore.
$\frac{ax_2^{}}{(1-a)x_1^{}}=\frac{ p_1}{ p_2}$
$\color{blue}{***}$
Solving for $x_2p_2$
$x_2p_2=\frac{p_1(1-\alpha)}{\alpha}x_1$
Inserting the term for $x_2p_2$ in the budget restriction:
$y=x_1p_1+p_2x_2$
$y=x_1p_1+\frac{p_1(1-\alpha)}{\alpha}x_1$
Factoring out $x_1$
$y=x_1\left( p_1+\frac{p_1(1-\alpha)}{\alpha} \right)\Rightarrow y=x_1p_1+\frac{x_1p_1}{\alpha} -x_1\frac{p_1\alpha}{\alpha} $
The third term is the negative of the first term:
$y=\frac{x_1p_1}{\alpha} \Rightarrow x_1=\alpha\frac{y}{p_1} $
For a given y and a given $p_2$ the maschallian demand function is $x_1(p_1,\overline{p}_2,\overline{y})=\alpha\frac{\overline y}{p_1} $
The same can be done for $x_2(p_2,\overline{p}_1,\overline{y})$. Repeat the similar steps after $\color{blue}{***}$.