You can also achieve the desired result using the formula below:
Sample Formula
=SUM(BYROW(A2:A, LAMBDA(x, IF(x<>"", x - INDEX(B:B, ROW(x)), 0))))
Sample Output
References:
BYROW
LAMBDA
INDEX