Find the maximun of the sum $\sum_{k=1}^{n}(f(f(k))-f(k))$

Let $f:\{1,2,3,\cdots,n\}\to \{1,2,3,\cdots,n\}$ such that $$f(1)\le f(2)\le\cdots\le f(n)$$ Let $g(n)$ $$g(n)=max\left(\sum_{k=1}^{n}(f(f(k))-f(k))\right)$$ Find $$g(n)$$


My best effort:

Let $m=\lceil \frac{n+1}{2}\rceil$. So $m=\frac{n+1}{2}$ for odd $n$, and $m=\frac{n+2}{2}$ for even $n$.

Then let $f(k)=m$ for $1 \le k \lt m$ and $f(k)=n$ for $m \le k \le n$. In particular $f(m)=f(n)=n$.

In this case $f(f(k))=n$ for all $k$ and so $\displaystyle \sum_{k=1}^n (f(f(k))-f(k)) = (m-1)(n-m).$

For odd $n$ this gives $\displaystyle \sum_{k=1}^n (f(f(k))-f(k)) = \left(\frac{n=1}{2}\right)^2 = \frac{n^2}{4}-\frac{n}{2}+\frac14.$

For even $n$ this gives $\displaystyle \sum_{k=1}^n (f(f(k))-f(k)) = \frac{n}{2} \times \frac{n-2}{2}= \frac{n^2}{4}-\frac{n}{2}.$