First, if $m<n(n+1)/2$ then the answer is clearly $k$.
Now suppose that $m\ge n(n+1)/2$, and note that $f(k,m,n)=k-(m-1)+f(m-1,m,n)$, so we just need to find $f(m-1,m,n)$.
If some $n$ elements in $S$ do sum up to $m$, then
\[n\min{S}+\frac{n(n-1)}{2}\le m\implies \min{S}\le\left\lfloor\frac{m}{n}-\frac{n-1}{2}\right\rfloor,\]so we have
\[f(m-1,m,n)\ge m-1-\left\lfloor\frac{m}{n}-\frac{n-1}{2}\right\rfloor=X.\]Now assume for the sake of contradiction that we have a set with $|S|\ge X+1$ and no $n$ distinct elements adding up to $m$ containing smallest elements
\[1\le a_1<a_2<\cdots<a_{n-2}\le\frac{m}{n}+\frac{n-7}{2}\](otherwise we clearly cannot fit $X+1-(n-2)$ numbers greater than $a_{n-2}$). Let
\[s=a_1+\cdots+a_{n-2}\le (n-2)a_{n-2}-\frac{(n-2)(n-3)}{2}\le\frac{(n-2)m}{n}-2(n-2).\]If $m-s=u+v$ for some $a_{n-2}<u<v<m-s-a_{n-2}$, then at most one of $u,v$ can be in $S$. We can check that $m-s-2a_{n-2}\ge n+3\ge0$, so
\begin{align*}
m-\left\lfloor\frac{m}{n}-\frac{n-1}{2}\right\rfloor = X+1 \le |S|
&\le n-2+\left\lfloor\frac{m-s-2a_{n-2}}{2}\right\rfloor+m-1-(m-s-a_{n-2}-1) \\
&= n-2+\left\lfloor\frac{m+s}{2}\right\rfloor \\
&\le \left\lfloor\frac{(n-1)m}{n}\right\rfloor,
\end{align*}a clear contradiction.