When thinking about energy flows, it is often useful to divide them into the flows into the system and the flows out of the system. The change to the system's total energy is then the difference between these two:
if more energy flows in, then the total energy increases
if more energy flows out, then the total energy decreases We can also write this as an equation: $$\Delta E_{total} = \Delta E_{in} - \Delta E_{out}$$
In this example, we have
IN: 40J + 30J = 70J
OUT: 80J + 20J = 100J
There is more energy flowing out, so the total energy of the system drops by $100-70 = 30{\rm J}$. (We might also say that $\Delta E=70-100=-30{\rm J}$.)