This formula worked on the sample data you supplied. Enter this in cell F11:
Then drag it down in column F.
If your data is more variable than you have shown, then this formula would need to be extended, to cope with more empty cells.
The formula basically works by testing column C and taking one of two paths depending on whether there is any data in column C on that row.
Both paths then test cells in column E in rows above, to find a cell containing data, and when a cell containing data is found the relevant subtraction is performed.
As you can see from the formula it only tests four rows above for data. If you have some cases where the gap in column E is more than 4 rows the formula will fail.
If you have gaps of more than 4 rows you could just add additional tests to find the last row above that contains data.