I am trying to optimize a function of the following form:

$L = \int_{t=0}^{T}(AR-x)dt$, where A is a system parameter

i.e. I am trying to find the optimum x(t) that minimizes L over all admissible x(t)s. R is related to x using a relation:

$\frac{dR}{dt} = axR axRY - bR$, where a and b are system parameters and $R(0) = R_{0}$.

$\frac{dY}{dt} = -xRY$

I was looking at this problem from mainly a simulation perspective. There is a whole amount of work that went into showing that x can take only two specific values that minimize the function over any given interval. Now, what I was thinking was to convert the integral into its discrete formulation and do the following:

For $t=1$, Let $x = x_{min}$ and calculate $L_{10}$ Let $x = x_{max}$ and calculate $L_{11}$ Finally, choose the one that has the min L.

And then continue with t=2 and so on, in the same way. If I visualize this problem, it is nothing but finding a minimum cost path in a binary tree i.e. something of the following form:

---------------------- $L_{00}$
-----------------------/--\---------
------------------$L_{10}$----$L_{11}$----
-----------------/-----\----/----\-------
-------------$L_{20}$-----$L_{21}$-$L_{22}$-$L_{23}$--

and so on until the last T. I am not sure if my thought process of simulating this is in the right direction. Can someone give me some suggestions?