How to fetch and edit multiple subform fields depending on the value of another subform field

How to fetch and edit multiple subform fields depending on the value of another subform field

Hi,

I have a main form "Add_Company_Details" which has a subform "Add_Bill".

Main Form "Add_Company_Details" contains:
      1. Balance (currency field)
      2. Subform "Add_Bill":
            1. Amount (currency field)
            2. Paid1 (Dropdown field with 2 options: "YES" or "NO")

The "Balance" field in the main form calculates the sum of all related "Amount" fields in the subform. I'm using a deluge script (on success) while a record is being created or edited in "Add_Company_Details":
  1. total = 0;
  2. for each  r in input.Add_Bill
  3. {
  4. total = total + ifnull(r.Amount,0);
  5. }
  6. input.Balance = total;
So far so good. What I'm trying to achieve now is:

1. If the subform field "Paid1" dropdown is set to "YES" the corresponding subform field "Amount" should be converted into a positive number
2. If the subform field "Paid1" dropdown is set to "NO" the corresponding subform field "Amount" should be converted into a negative number
3. The main form field "Balance" should only sum up "Amount" fields that contain a negative number or where the "Paid1" field is set to "NO".

I've tried multiple "For each" scripts but had no luck so far. 

Any help is appreciated.

Thanks