Explanation

This is a pretty standard use of the SUMIFS function.

Excel SUMIFS function