Try this formula in L5 copied down

=IF(K5=0,"",B5+E5-LOOKUP(2,1/(K$4:K4<>0),B$4:B4)-LOOKUP(2,1/(K$4:K4<>0),J$4:J4))