I think you'd have to try every single combination to do this, which can add up a lot:
In you exemple [A, B, C, D] for a value Z, you have to compare I think 16 sums, there may be a way to do it (like excluding some of them if they cannot work, for exemple if the sum of all positives values cannot reach the number or above it, then no sum will be able to), but the basic concept is this I think. I hope there is an easier way
you could maybe try a repeat loop (16 times):
compare Z = A*((int(loopindex/8))%2) + B*((int(loopindex/4))%2) + C*((int(loopindex/2))%2) + D*(loopindex%2)
intested but this should try all the sums, (it will also compare Z to 0 the first time)
EDIT: glad you figured it out, I should have though of using not only one loop though.