suppose I have a few lists

```
b_wi=((1,2,3,4),(6,7,8,9,10,11)) #b_wi is a subset of x
f_wi=((5,4,2,7,9),(5,4,3,7,2,3,4))
x=((1,2,3,4,5,6,7,8,9,2,5,3),(1,24,36,42,35,6,7,8,91,2,5,3))
#the following two are step functions formed by the above lists.
'''
F1 = f_wi(0)(0) if x< b_wi(0)(0) ;
f_wi(0)(1) if x< b_wi(0)(0) <=x< b_wi(0)(1);
...;
f_wi(0)(-1) if x>= b_wi(1)(-1)
F2 = f_wi(1)(0) if x< b_wi(1)(0) ;
f_wi(1)(1) if x< b_wi(1)(0) <=x< b_wi(1)(1);
...;
f_wi(1)(-1) if x>= b_wi(1)(-1)
'''
```

Now I want to get max (F1+F2) and the corresponding interval. I did some searching and found this : Evaluate sum of step functions

However, since the length of intervals is not the same for these step functions, I cannot apply the solution in the link directly. Instead, I did this:

```
import numpy as np
from pandas.core.common import flatten
def func(x,b,f):
return f(np.searchsorted(b,x,side='right'))
intval= np.unique(list(flatten(b_wi)))
x=np.concatenate(((-10000),(intval(:-1)+intval(1:))/2,(10000))) #b_wi is a subset of x. That is why I can use this.
a=np.zeros((len(x)))
for b, f in zip(b_wi,f_wi):
a=a+ func(x,b,np.asarray(f))
print(a/2)
```

Now I get can get the maximum of (F1+F2) using

```
np.amax(a)
```

and I can get the interval as well.

This is just a simple example I used to illustrate my question. My actual lists are longer than these and there are 100000 step functions. Since I ‘flatten’ ‘b_wi’ in order to find the corresponding interval, the length of ‘intval’ becomes too large. Hence, my method is too slow. Does anyone know how I could speed it up? I feel like I am using the wrong method. Thank you very much.