You didn't mention changing values in column A but I suspected that would be the case. Try this:

Formula: copy to clipboard
=A2-SUMIF($A$2:A2,A2,$B$2:B2)