In answering this question, I thought about working out a closed-form formula for $f(n)$ there. I got as far as writing:
$$ f(n) = \sum_{k=0}^{n}2^{k}(n-k) $$
…but I wasn't sure how to go farther. I plugged this into Wolfram Alpha and it spat out this:
$$\sum_{k=0}^{n}2^{k}(n-k) = 2^{n+1} - n - 2$$
I've been staring at this for a while but I don't see how to derive the right-hand side from the left-hand side. Would someone be willing to clarify this for me?