Question: Prove or disprove.

Suppose $X$ is a compact metric space and $f: X \rightarrow \mathbb{R}$ is a function for which $f^{-1}([t,\infty))$ is closed for any real $t$. Then $f$ achieves its maximum value on $X$. 

I believe this is false since $f$ is not assumed to be continuous. But since the preimage of that subset of the range is closed, it makes me think that maybe it does indeed reach a maximum. I'm quite stumped. Any nudges in the right direction would be terrific.

Answer: 
First we prove that $f$ is bounded above. If not, there is a sequence of points $x\_i$ such that $f(x\_i) > i$ for all $i$. $x\_i$, being an infinite sequence, has a convergent subsequence with limit $x$, which is contained in $f^{-1}([i,\infty))$ for all $i$, which is closed. But $\bigcap [i,\infty)$ is empty, so this cannot be the case, and we have a contradiction. Thus $f$ is bounded above. 

Now let $A = \sup f$. Choose a sequence $x\_i$ such that $A-f(x\_i) < 2^{-i}$ for all $i$. Then this has a convergent subsequence with limit $x$, which is contained in $f^{-1}([A-2^{-i},\infty))$ for all $i$, hence is contained in $f^{-1}([A,\infty))$. Thus $f(x)=A$.
If $f$ is assumed to be bounded above thee its maximum is attained: let $M$ be teh supremum of $f$ on $X$. Then for each $n$ there exists $x\_n\in X$ such that $f(x\_n) >M-\frac 1 n$. By compactness there is a subsequence of $\{x\_n\}$ which converges to some $x$. Now $x\_m \in f^{-1}[M-\frac 1 n, \infty)$ for all $m \geq n$. By hypothesis this implies $x \in f^{-1}[M-\frac 1 n, \infty)$ which means $f(x) \geq M-\frac 1 n$. Since this is true for each $n$ we get $f(x)=M$. 
Actually, characterization of compactness by finite intersection property shows that $f$ is necessarily bounded. [Consider the compact sets $f^{-1}[n,\infty)$].