def merge(a, b):
    l1 = [(t, value, 1) for t, value in a]
l2 = [(t, value, 2) for t, value in b]
events = l1 + l2
events.sort()
last_time = -1
result = []
c1 = 0
c2 = 0
for t, value, index in events:
    if index == 1:
        c1 = value
if index == 2:
    c2 = value
if t == last_time:
    result.pop()
result.append((t, c1 + c2))
last_time = t
return result
Merge two lists of tuples with timestamps and queue lengths