Let $a_1,a_2,...,a_n>0$ be such that $\sum_{k=1}^n a_k=\frac{1}{2}n(n+1)$ 
Let $a_1,a_2,...,a_n>0$ be such that $\sum_{k=1}^n a_k=\frac{1}{2}n(n+1)$, then least value of
$$\sum_{k=1}^n\frac{(k^2-1)a_k+k^2+2k}{a_k^2+a_k+1}\text{ is,}$$

What I tried:
$$\sum_{k=1}^{n}k=\frac{1}{2}n(n+1)\implies k=a_k\tag{$\because\sum_{k=1}^n a_k=\frac{1}{2}n(n+1)$}$$
So replacing $a_k$ with $k$,
$$\begin{aligned}\require{cancel}
\sum_{k=1}^n\frac{(k^2-1)a_k+k^2+2k}{a_k^2+a_k+1}&=\sum_{k=1}^n\frac{(k^2-1)k+k^2+2k}{k^2+k+1}\\
&=\sum_{k=1}^{n}\frac{k^3-k+k^2+2k}{k^2+k+1}\\
&=\sum_{k=1}^{n}\frac{k^3+k+k^2}{k^2+k+1}\\
&=\sum_{k=1}^{n}\frac{k\color{red}{\bcancel{(k^2+k+1)}}}{\color{red}{\bcancel{(k^2+k+1)}}}\\
&=\sum_{k=1}^{n}k=\frac{1}{2}n(n+1)
\end{aligned}$$
Is this a correct method to solve this problem? because something feels off with this as the question asks for the least value but I get a direct result.
Please provide a correct method to solve if this is wrong

You can use the fact that
$$ a_n=\sum_{k=1}^n a_k-\sum_{k=1}^{n-1} a_k=\frac{1}{2}n(n+1)-\frac{1}{2}(n-1)n=n. $$
The rest is correct.
