Is such a function measurable? Let $(f_n)$ be a sequence of positive measurable functions defined on the same measurable space $(X, S)$. Let
for $t>0$ a function $g_t$ be given by: 
$ g_t(x)=\sup\{n \in \mathbb N: f_1(x)+...+f_n(x) \leq t \} $  if $f_1(x) \leq t$, $x\in X$,
and
$g_t(x)=0$   if $f_1(x)>t$, $x\in X$.
Is $g_t$ measurable?
 A: I think it is.
We can define a function $h_{t,n}(x)$ wich is $n$ if $f_1(x) +... +f_n(x) \leq t$ and $0$ otherwise. Then each $h_{t,n}$ is measurable (because $f_1(x)+...+f_n(x)$ is measurable for each $n$, and since $g_t$ is the supremum over the $h_{t,n}$,  it is measurable too.
(i hope i did not made a mistake here)
A: Here's a start. Fix $t>0$. You need to show that $A_k:=\{x\in X: g_t(x) =k\}$ is an element of $S$, for each positive integer $k$. But
$$
A_k = \{x\in X: h_{t,k}(x)\le t\}\cap \{x\in X: h_{t,k+1}(x)>t\},
$$
where I have used the notation $h_{t,n}(x) :=f_1(x)+\cdots+f_n(x)$ of supinf.
A: I think @supinf answer is correct. Here it is a direct representation of $g_t$ in terms of a countable sum of measurable functions. Let $S_n=\sum^nf_i$, then each $S_n$ is measurable
\begin{equation}
g_t(x)=\sum_{i\ge0} \left\{ S_i(x) \le t \right\},
\end{equation}
 where the the sets represent their indicator function. and I think it would not be necessary to define the $g_t(x)=0$ case independently. 
